@convai/web-sdk 1.9.0 → 1.9.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 +30 -28
- package/dist/core/ConvaiClient.js +10 -1
- package/dist/core/types.d.ts +13 -3
- package/dist/core/websocketUrl.d.ts +11 -0
- package/dist/core/websocketUrl.js +17 -0
- package/dist/utils/inlineMarkdown.d.ts +7 -5
- package/dist/utils/inlineMarkdown.js +7 -5
- package/dist/vanilla/WebSocketSession.d.ts +19 -13
- package/dist/vanilla/WebSocketSession.js +58 -14
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,10 +15,10 @@ TypeScript-first SDK for embedding Convai AI characters into React and vanilla J
|
|
|
15
15
|
> **Breaking for 1.8.0 users:** the `<convai-chat>` custom element and the `./embed/browser` entry point (`dist/embed/chat-embed-v1.js`) are removed. The copy/paste published-chat embed is now `<convai-widget>` from [`@convai/web-widget`](https://www.npmjs.com/package/@convai/web-widget); the [CHANGELOG](./CHANGELOG.md) has the before/after snippet.
|
|
16
16
|
|
|
17
17
|
- **Multi-character rooms** — connect a roster of characters with the `characters` config option (two copies of one character are two independent members), switch who answers with `setInteractionTarget()`, add or remove members live with `updateCharacterRoster()`, and let other participants join the same room with `joinRoom()`. Every transcript line, audio track and readiness event is attributed to a `membershipId`. [Usage below.](#multi-character-rooms-beta)
|
|
18
|
-
- **Typed events** — `client.on(event, callback)` infers each callback's payload from the exported `ConvaiEventMap`, so `botReady`, `modelOutput`, `characterStatus` and the rest are checked at compile time. See the [events reference](docs/
|
|
18
|
+
- **Typed events** — `client.on(event, callback)` infers each callback's payload from the exported `ConvaiEventMap`, so `botReady`, `modelOutput`, `characterStatus` and the rest are checked at compile time. See the [events reference](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/event-reference).
|
|
19
19
|
- **Local transcript lines and usage updates** — `appendMessage()` inserts a line into the transcript without sending anything, and `toggleUsageUpdates(true)` opts into per-turn `usageUpdate` cost breakdowns (on a session connected with `debug: true`; otherwise the server has no usage stream).
|
|
20
20
|
- **Character versioning** — connect to a character's editable draft, its promoted latest release, or an immutable tag with the `characterVersion` config option, and list, compare, release, promote, fork and discard versions through `client.characterVersions`. [Usage below.](#character-versioning-beta)
|
|
21
|
-
- **SSE interaction transport** — chat over the interaction API, selected by `interactionApiUrl`: text in, the reply streamed as text and, with `ttsEnabled`, as speech (`audioData`); the one transport that runs under Node. We recommend SSE for chat and realtime for voice — see [Choosing a Transport](docs/
|
|
21
|
+
- **SSE interaction transport** — chat over the interaction API, selected by `interactionApiUrl`: text in, the reply streamed as text and, with `ttsEnabled`, as speech (`audioData`); the one transport that runs under Node. We recommend SSE for chat and realtime for voice — see [Choosing a Transport](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/choose-a-transport). [Usage below.](#sse-interaction-transport-beta)
|
|
22
22
|
- **Embeddable vanilla widget** — the vanilla widget runs inside a shadow root, with a character header and a connecting overlay, so it can be dropped into any page.
|
|
23
23
|
- **Node-importable dist** — `@convai/web-sdk/core`, `/vanilla`, `/vanilla/websocket` and `/lipsync-helpers` load in plain Node, SSR and Next.js server components.
|
|
24
24
|
- **Character voice mute** — `setCharacterMuted()` / `isCharacterMuted()` silence the character's voice for the listener; `audioControls` stays the microphone.
|
|
@@ -34,7 +34,7 @@ TypeScript-first SDK for embedding Convai AI characters into React and vanilla J
|
|
|
34
34
|
connection, so a half-upgraded backend fails at connect rather than
|
|
35
35
|
mid-conversation. Omit the block entirely and nothing changes: existing
|
|
36
36
|
connections and one-argument text messages stay on the v1 wire contract.
|
|
37
|
-
[Options table.](
|
|
37
|
+
[Options table.](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/actions)
|
|
38
38
|
- **Correlated action results and logical turns** — action results carry the
|
|
39
39
|
call they answer, and turn correlation holds across multi-envelope and
|
|
40
40
|
voice turns, so a response can be tied to its cause without guessing from
|
|
@@ -137,7 +137,7 @@ window.addEventListener("beforeunload", () => {
|
|
|
137
137
|
|
|
138
138
|
### Published text chat
|
|
139
139
|
|
|
140
|
-
See the [Published text chat guide](
|
|
140
|
+
See the [Published text chat guide](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/published-text-chat) for
|
|
141
141
|
React and vanilla integrations, lifecycle behavior, and the complete API
|
|
142
142
|
reference.
|
|
143
143
|
|
|
@@ -170,10 +170,11 @@ session. An application performing its own retry may pass the same
|
|
|
170
170
|
text-only; the server-issued room token does not permit publishing microphone
|
|
171
171
|
or camera tracks.
|
|
172
172
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
not reuse a process-wide
|
|
173
|
+
In a browser, the `@convai/web-sdk/embed` grant helper
|
|
174
|
+
(`requestPublishedChatGrant`) creates its own per-browser requester identifier.
|
|
175
|
+
If a server or SSR integration calls the published-chat grant helpers directly,
|
|
176
|
+
it must pass a caller-scoped requester identifier; do not reuse a process-wide
|
|
177
|
+
identifier across visitors.
|
|
177
178
|
|
|
178
179
|
When using `ConvaiWidget`, provide `requestPublicationGrant` so Reset,
|
|
179
180
|
Disconnect, and later reopen operations mint a new grant instead of attempting
|
|
@@ -247,27 +248,28 @@ Full documentation is at **<a href="https://docs.convai.com/api-docs/plugins-and
|
|
|
247
248
|
|
|
248
249
|
| Guide | Description |
|
|
249
250
|
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------- |
|
|
250
|
-
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk" target="_blank" rel="noopener noreferrer">Quick Start</a> | First working integration in under 5 minutes |
|
|
251
|
-
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk" target="_blank" rel="noopener noreferrer">Configuration</a> | All `ConvaiConfig` options |
|
|
252
|
-
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk" target="_blank" rel="noopener noreferrer">React Integration</a> | `useConvaiClient`, `ConvaiWidget`, and React-specific patterns |
|
|
253
|
-
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk" target="_blank" rel="noopener noreferrer">Vanilla JS</a> | `ConvaiClient`, `createConvaiWidget`, and audio setup |
|
|
251
|
+
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/quickstart" target="_blank" rel="noopener noreferrer">Quick Start</a> | First working integration in under 5 minutes |
|
|
252
|
+
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/vanilla-typescript/best-practices-and-type-definitions" target="_blank" rel="noopener noreferrer">Configuration</a> | All `ConvaiConfig` options |
|
|
253
|
+
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/react" target="_blank" rel="noopener noreferrer">React Integration</a> | `useConvaiClient`, `ConvaiWidget`, and React-specific patterns |
|
|
254
|
+
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/vanilla-typescript" target="_blank" rel="noopener noreferrer">Vanilla JS</a> | `ConvaiClient`, `createConvaiWidget`, and audio setup |
|
|
254
255
|
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/event-reference" target="_blank" rel="noopener noreferrer">Events</a> | Full event reference — `botReady`, `stateChange`, `messagesChange`, `interactionCreated`, and more |
|
|
255
256
|
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/dynamic-context" target="_blank" rel="noopener noreferrer">Context Management</a> | Dynamic context, `updateContext`, file upload, session management |
|
|
256
257
|
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/emotions" target="_blank" rel="noopener noreferrer">Emotions</a> | Per-turn emotion detection, provider options, and `stateOfMind` / `updateEmotion()` |
|
|
257
|
-
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk" target="_blank" rel="noopener noreferrer">Lipsync</a> | ARKit / MetaHuman blendshape streams and `BlendshapeQueue` API |
|
|
258
|
+
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/lipsync-and-blendshape" target="_blank" rel="noopener noreferrer">Lipsync</a> | ARKit / MetaHuman blendshape streams and `BlendshapeQueue` API |
|
|
258
259
|
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/actions" target="_blank" rel="noopener noreferrer">Actions</a> | Trigger character behaviors and scene actions |
|
|
259
260
|
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/long-term-memory" target="_blank" rel="noopener noreferrer">Memory</a> | Long-term memory scoped to end users |
|
|
260
|
-
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk" target="_blank" rel="noopener noreferrer">Audio & Video</a> | Microphone, camera, screen share controls |
|
|
261
|
+
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/react" target="_blank" rel="noopener noreferrer">Audio & Video</a> | Microphone, camera, screen share controls |
|
|
261
262
|
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/error-handling" target="_blank" rel="noopener noreferrer">Error Handling</a> | `error`, `disconnect`, `serverResponse`, retry patterns |
|
|
262
263
|
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/auth-tokens" target="_blank" rel="noopener noreferrer">Auth Tokens</a> | Server-side token exchange for production |
|
|
263
264
|
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/websocket-transport-layer" target="_blank" rel="noopener noreferrer">WebSocket Transport</a> | Alternative transport for WebRTC-constrained environments |
|
|
264
|
-
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk" target="_blank" rel="noopener noreferrer">SSE Transport</a> | Chat transport: text in, text or audio out; the one that runs under Node |
|
|
265
|
-
| [Choosing a Transport](docs/
|
|
266
|
-
| [Character Versioning](docs/
|
|
267
|
-
| [Multi-Character Rooms](docs/
|
|
268
|
-
| [Published Text Chat](docs/
|
|
269
|
-
| [Link Previews](docs/
|
|
270
|
-
| [
|
|
265
|
+
| <a href="https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/sse-transport" target="_blank" rel="noopener noreferrer">SSE Transport</a> | Chat transport: text in, text or audio out; the one that runs under Node |
|
|
266
|
+
| [Choosing a Transport](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/choose-a-transport) | SSE vs realtime vs WebSocket, the recommended SSE-chat-plus-voice-mode setup, and why |
|
|
267
|
+
| [Character Versioning](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/character-versioning) | Connect to a draft, latest, or tagged version; list, compare, release and promote versions |
|
|
268
|
+
| [Multi-Character Rooms](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/multi-character-rooms) | Rosters, `joinRoom`, interaction targets, live roster updates, per-member attribution |
|
|
269
|
+
| [Published Text Chat](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/published-text-chat) | Grant-based text-only sessions, the copy/paste embed, lifecycle and API reference |
|
|
270
|
+
| [Link Previews](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/link-previews) | Rendering links the character sends: previews, custom chat UIs, the scheme allowlist |
|
|
271
|
+
| [Mute the Character's Voice](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/mute-the-characters-voice) | `setCharacterMuted` / `isCharacterMuted`: silence the character for the listener |
|
|
272
|
+
| [Raw Transport Messages](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/raw-messages) | The typed `rawMessage` event across LiveKit, WebSocket and SSE |
|
|
271
273
|
|
|
272
274
|
---
|
|
273
275
|
|
|
@@ -343,7 +345,7 @@ events on the same response; there is no microphone, video, or lipsync. It never
|
|
|
343
345
|
interaction via the `characterSessionId` event.
|
|
344
346
|
|
|
345
347
|
We recommend SSE for chat and realtime for voice: start on SSE and move the same conversation to
|
|
346
|
-
realtime when the user wants to talk. See [Choosing a Transport](docs/
|
|
348
|
+
realtime when the user wants to talk. See [Choosing a Transport](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/choose-a-transport)
|
|
347
349
|
for why, and for building on one transport only.
|
|
348
350
|
|
|
349
351
|
Because it needs neither WebRTC nor a DOM, this is the only transport that runs outside a browser.
|
|
@@ -351,7 +353,7 @@ Import `@convai/web-sdk/core` from Node, SSR, or a serverless function — not t
|
|
|
351
353
|
entry points, which pull in browser-only dependencies.
|
|
352
354
|
|
|
353
355
|
The interaction API is currently reachable at staging only; a production host is planned.
|
|
354
|
-
Full guide: [SSE Interaction Transport](docs/
|
|
356
|
+
Full guide: [SSE Interaction Transport](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/sse-transport).
|
|
355
357
|
|
|
356
358
|
---
|
|
357
359
|
|
|
@@ -398,9 +400,9 @@ personal access token; every method rejects with `CharacterApiError` (`status`,
|
|
|
398
400
|
non-2xx answer. Point `characterApiUrl` at `https://api2-stg.convai.com` to author against
|
|
399
401
|
staging.
|
|
400
402
|
|
|
401
|
-
Explicit selectors are resolved by the runtime through the Character REST platform
|
|
402
|
-
|
|
403
|
-
Full guide: [Character Versioning](docs/
|
|
403
|
+
Explicit selectors are resolved by the runtime through the Character REST platform, on both
|
|
404
|
+
production and staging. A runtime without version resolution answers them with `503`.
|
|
405
|
+
Full guide: [Character Versioning](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/character-versioning).
|
|
404
406
|
|
|
405
407
|
---
|
|
406
408
|
|
|
@@ -446,11 +448,11 @@ Key points:
|
|
|
446
448
|
|
|
447
449
|
- `characters` and `characterId` are mutually exclusive, and roster entries take a bare character UUID (no `-draft` / `-1.2` selectors).
|
|
448
450
|
- `provisioningStatus` describes dispatch only; a member is usable when its own `characterReady` arrives.
|
|
449
|
-
- Transcript rows, `characterAudioTrack` events and `
|
|
451
|
+
- Transcript rows, `characterAudioTrack` events and `characterReady` payloads carry `membershipId`, so two copies of one character never merge. (`botReady` takes no arguments.)
|
|
450
452
|
- Room-topology failures throw `ConvaiRoomError` with `status`, `code`, `requestTraceId` and `isRetryable`; the SDK retries `ROSTER_PROVISIONING_IN_PROGRESS` for you (`rosterProvisioningRetry`).
|
|
451
453
|
- Action protocol v2 and `actionConfig.tools` are single-character only and cannot be combined with a roster.
|
|
452
454
|
|
|
453
|
-
Full reference: [
|
|
455
|
+
Full reference: [Host several characters in one room](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/multi-character-rooms).
|
|
454
456
|
|
|
455
457
|
---
|
|
456
458
|
|
|
@@ -2,6 +2,7 @@ import { Room, RoomEvent, ConnectionState, DefaultReconnectPolicy, setLogLevel,
|
|
|
2
2
|
setLogLevel("warn");
|
|
3
3
|
import { DisconnectReasonEnum, } from "./types.js";
|
|
4
4
|
import { AudioManager } from "./AudioManager.js";
|
|
5
|
+
import { secureWebSocketUrl } from "./websocketUrl.js";
|
|
5
6
|
import { VideoManager } from "./VideoManager.js";
|
|
6
7
|
import { ScreenShareManager } from "./ScreenShareManager.js";
|
|
7
8
|
import { MessageHandler } from "./MessageHandler.js";
|
|
@@ -435,6 +436,9 @@ export class ConvaiClient extends EventEmitter {
|
|
|
435
436
|
// session-wide setting is the only one that can apply to it.
|
|
436
437
|
this._wsBotAudioTrack.enabled = !this._characterMuted;
|
|
437
438
|
}
|
|
439
|
+
// Pipecat's WebSocket transport plays the character's audio itself and
|
|
440
|
+
// exposes no track, so the session silences its own player instead.
|
|
441
|
+
this._wsSession?.setBotAudioMuted?.(this._characterMuted);
|
|
438
442
|
if (this._room.state !== ConnectionState.Connected)
|
|
439
443
|
return;
|
|
440
444
|
this._room.remoteParticipants.forEach((participant) => {
|
|
@@ -1562,7 +1566,9 @@ export class ConvaiClient extends EventEmitter {
|
|
|
1562
1566
|
if (transportType === "websocket") {
|
|
1563
1567
|
// ── WebSocket (Pipecat) transport path ────────────────────────────
|
|
1564
1568
|
// Server returns the WebSocket URL in room_url (same field as LiveKit).
|
|
1565
|
-
|
|
1569
|
+
// Hosted Convai returns ws:// for a TLS-only endpoint; see websocketUrl.ts.
|
|
1570
|
+
const roomUrl = connectionData.room_url;
|
|
1571
|
+
const wsUrl = roomUrl && secureWebSocketUrl(roomUrl, configWithDefaults.url);
|
|
1566
1572
|
if (!wsUrl) {
|
|
1567
1573
|
throw new Error("No WebSocket URL returned from /connect endpoint (expected room_url)");
|
|
1568
1574
|
}
|
|
@@ -1575,6 +1581,9 @@ export class ConvaiClient extends EventEmitter {
|
|
|
1575
1581
|
// after the connection is established (enableMic(false) works once the
|
|
1576
1582
|
// stream exists, but not before it is initialized).
|
|
1577
1583
|
this._wsSession = _wsFactory((payload) => this._messageHandler.handleDataReceivedPublic(payload), true);
|
|
1584
|
+
// The mute setting outlives sessions: a reconnect must not hand back an
|
|
1585
|
+
// audible character.
|
|
1586
|
+
this._wsSession.setBotAudioMuted?.(this._characterMuted);
|
|
1578
1587
|
const webSocketVisionEnabled = configWithDefaults.enableVideo === true &&
|
|
1579
1588
|
effectiveVisionInputConfig?.enabled !== false;
|
|
1580
1589
|
if (webSocketVisionEnabled) {
|
package/dist/core/types.d.ts
CHANGED
|
@@ -72,6 +72,12 @@ export interface IWebSocketSession {
|
|
|
72
72
|
disconnect(): Promise<void>;
|
|
73
73
|
sendMessage(type: string, data: unknown): void;
|
|
74
74
|
enableMic(enable: boolean): void;
|
|
75
|
+
/**
|
|
76
|
+
* Silence (or restore) the character's audio for the listener. Optional: an
|
|
77
|
+
* implementation that exposes the bot audio as a track can leave it out and
|
|
78
|
+
* let `botAudioTrack` carry the mute instead.
|
|
79
|
+
*/
|
|
80
|
+
setBotAudioMuted?(muted: boolean): void;
|
|
75
81
|
on(event: string, listener: (...args: any[]) => void): () => void;
|
|
76
82
|
off(event: string, listener: (...args: any[]) => void): void;
|
|
77
83
|
}
|
|
@@ -922,9 +928,13 @@ export interface ConvaiConfig {
|
|
|
922
928
|
* which also drops link targets: `[Doc](https://…/a.pdf)` reaches the client
|
|
923
929
|
* as `Doc`. With this on, the session preserves `[text](url)`, ``
|
|
924
930
|
* and bare `http(s)://` URLs verbatim (trailing sentence punctuation stays
|
|
925
|
-
* outside the URL,
|
|
926
|
-
*
|
|
927
|
-
*
|
|
931
|
+
* outside the URL) while bold, headings, tables and code are flattened as
|
|
932
|
+
* before. Speech is unaffected — URLs are never read aloud.
|
|
933
|
+
*
|
|
934
|
+
* The server does not sanitise schemes: a markdown link keeps its target
|
|
935
|
+
* whatever the scheme, `[x](javascript:…)` included (only bare URLs are
|
|
936
|
+
* limited to http(s)). The SDK's renderers allow only safe schemes; a custom
|
|
937
|
+
* renderer must check every href with `isSafeHref`.
|
|
928
938
|
*
|
|
929
939
|
* Both widgets render the preserved form as clickable links, with inline
|
|
930
940
|
* previews for image and video URLs. If you render `chatMessages` yourself,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The WebSocket URL from a `/connect` response, made safe to open.
|
|
3
|
+
*
|
|
4
|
+
* Hosted Convai answers `/connect` over https but returns `ws://…/chat` in
|
|
5
|
+
* `room_url`, and that endpoint only accepts TLS: a plain `ws://` socket is
|
|
6
|
+
* refused (the browser reports close code 1006). A `ws://` URL is upgraded to
|
|
7
|
+
* `wss://` when the `/connect` URL was https, or when the page itself is https,
|
|
8
|
+
* where the browser would block an insecure socket anyway. Local development
|
|
9
|
+
* over http keeps `ws://`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function secureWebSocketUrl(roomUrl: string, connectUrl?: string | null, pageProtocol?: string | undefined): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The WebSocket URL from a `/connect` response, made safe to open.
|
|
3
|
+
*
|
|
4
|
+
* Hosted Convai answers `/connect` over https but returns `ws://…/chat` in
|
|
5
|
+
* `room_url`, and that endpoint only accepts TLS: a plain `ws://` socket is
|
|
6
|
+
* refused (the browser reports close code 1006). A `ws://` URL is upgraded to
|
|
7
|
+
* `wss://` when the `/connect` URL was https, or when the page itself is https,
|
|
8
|
+
* where the browser would block an insecure socket anyway. Local development
|
|
9
|
+
* over http keeps `ws://`.
|
|
10
|
+
*/
|
|
11
|
+
export function secureWebSocketUrl(roomUrl, connectUrl, pageProtocol = typeof location !== "undefined" ? location.protocol : undefined) {
|
|
12
|
+
if (!/^ws:\/\//i.test(roomUrl))
|
|
13
|
+
return roomUrl;
|
|
14
|
+
const secureConnect = typeof connectUrl === "string" && /^https:\/\//i.test(connectUrl);
|
|
15
|
+
const securePage = pageProtocol === "https:";
|
|
16
|
+
return secureConnect || securePage ? roomUrl.replace(/^ws:\/\//i, "wss://") : roomUrl;
|
|
17
|
+
}
|
|
@@ -3,12 +3,14 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Core-service can hand the client bot text with links preserved
|
|
5
5
|
* (`preserve_links_in_output` on /connect): `[text](url)`, `` and
|
|
6
|
-
* bare `http(s)://` URLs arrive verbatim,
|
|
7
|
-
* server-side.
|
|
8
|
-
* map onto DOM or JSX.
|
|
6
|
+
* bare `http(s)://` URLs arrive verbatim, other markdown is still flattened
|
|
7
|
+
* server-side. A markdown link keeps its target whatever the scheme. This
|
|
8
|
+
* turns one line of that text into nodes a renderer can map onto DOM or JSX.
|
|
9
9
|
*
|
|
10
|
-
* Only
|
|
11
|
-
*
|
|
10
|
+
* Only the schemes in `SAFE_SCHEMES` (http, https, ftp, ftps, mailto, tel,
|
|
11
|
+
* sms) become links or images. Anything else — including
|
|
12
|
+
* `javascript:` and `data:` — becomes a `blocked` node, regardless of what the
|
|
13
|
+
* server sent.
|
|
12
14
|
* A link whose closing paren has not streamed in yet is left as text too, so
|
|
13
15
|
* a bubble does not flicker between shapes mid-response.
|
|
14
16
|
*/
|
|
@@ -3,12 +3,14 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Core-service can hand the client bot text with links preserved
|
|
5
5
|
* (`preserve_links_in_output` on /connect): `[text](url)`, `` and
|
|
6
|
-
* bare `http(s)://` URLs arrive verbatim,
|
|
7
|
-
* server-side.
|
|
8
|
-
* map onto DOM or JSX.
|
|
6
|
+
* bare `http(s)://` URLs arrive verbatim, other markdown is still flattened
|
|
7
|
+
* server-side. A markdown link keeps its target whatever the scheme. This
|
|
8
|
+
* turns one line of that text into nodes a renderer can map onto DOM or JSX.
|
|
9
9
|
*
|
|
10
|
-
* Only
|
|
11
|
-
*
|
|
10
|
+
* Only the schemes in `SAFE_SCHEMES` (http, https, ftp, ftps, mailto, tel,
|
|
11
|
+
* sms) become links or images. Anything else — including
|
|
12
|
+
* `javascript:` and `data:` — becomes a `blocked` node, regardless of what the
|
|
13
|
+
* server sent.
|
|
12
14
|
* A link whose closing paren has not streamed in yet is left as text too, so
|
|
13
15
|
* a bubble does not flicker between shapes mid-response.
|
|
14
16
|
*/
|
|
@@ -1,21 +1,12 @@
|
|
|
1
1
|
import { EventEmitter } from "../core/EventEmitter.js";
|
|
2
|
-
/**
|
|
3
|
-
* Adapter that wraps PipecatClient + WebSocketTransport for use in ConvaiClient.
|
|
4
|
-
*
|
|
5
|
-
* Responsibilities:
|
|
6
|
-
* - Manage PipecatClient lifecycle (connect, disconnect)
|
|
7
|
-
* - Forward all RTVI events back to MessageHandler by re-encoding them as {type, data} payloads
|
|
8
|
-
* - Send outgoing messages via PipecatClient.sendClientMessage (Pipecat wraps to {t, d} envelope)
|
|
9
|
-
* - Expose mic control and bot audio track
|
|
10
|
-
*
|
|
11
|
-
* The caller is responsible for making the HTTP /connect request.
|
|
12
|
-
* Pass the parsed response object to connect() — WebSocketTransport extracts
|
|
13
|
-
* wsUrl/ws_url automatically.
|
|
14
|
-
*/
|
|
15
2
|
export declare class WebSocketSession extends EventEmitter {
|
|
16
3
|
private _pcClient;
|
|
4
|
+
private readonly _transport;
|
|
17
5
|
private _isConnected;
|
|
18
6
|
private readonly _onMessage;
|
|
7
|
+
private _botAudioMuted;
|
|
8
|
+
private _botGain;
|
|
9
|
+
private _player;
|
|
19
10
|
constructor(onMessage: (payload: Uint8Array) => void, enableMic?: boolean);
|
|
20
11
|
get isConnected(): boolean;
|
|
21
12
|
/**
|
|
@@ -38,6 +29,21 @@ export declare class WebSocketSession extends EventEmitter {
|
|
|
38
29
|
* silence (no speech), and Daily's internal state never changes.
|
|
39
30
|
*/
|
|
40
31
|
enableMic(enable: boolean): void;
|
|
32
|
+
/**
|
|
33
|
+
* Silence (or restore) the character's audio for the listener. The
|
|
34
|
+
* character keeps speaking; what it says while muted is dropped, not queued.
|
|
35
|
+
*/
|
|
36
|
+
setBotAudioMuted(muted: boolean): void;
|
|
37
|
+
/**
|
|
38
|
+
* Pipecat's WebSocket transport plays the character through its own
|
|
39
|
+
* WavStreamPlayer, wiring each new stream straight to the speakers, and
|
|
40
|
+
* exposes no track to mute. Wrap the player's stream start so every stream
|
|
41
|
+
* goes through a GainNode instead. These are Pipecat internals, so each one
|
|
42
|
+
* is feature-checked: if they are missing, mute degrades to a warning rather
|
|
43
|
+
* than breaking playback.
|
|
44
|
+
*/
|
|
45
|
+
private _routePlayerThroughGain;
|
|
46
|
+
private _attachGain;
|
|
41
47
|
getBotAudioTrack(): MediaStreamTrack | null;
|
|
42
48
|
private _getLocalAudioTrack;
|
|
43
49
|
private _setupMessageForwarding;
|
|
@@ -1,26 +1,17 @@
|
|
|
1
1
|
import { PipecatClient, RTVIEvent } from "@pipecat-ai/client-js";
|
|
2
2
|
import { WebSocketTransport } from "@pipecat-ai/websocket-transport";
|
|
3
3
|
import { EventEmitter } from "../core/EventEmitter.js";
|
|
4
|
-
/**
|
|
5
|
-
* Adapter that wraps PipecatClient + WebSocketTransport for use in ConvaiClient.
|
|
6
|
-
*
|
|
7
|
-
* Responsibilities:
|
|
8
|
-
* - Manage PipecatClient lifecycle (connect, disconnect)
|
|
9
|
-
* - Forward all RTVI events back to MessageHandler by re-encoding them as {type, data} payloads
|
|
10
|
-
* - Send outgoing messages via PipecatClient.sendClientMessage (Pipecat wraps to {t, d} envelope)
|
|
11
|
-
* - Expose mic control and bot audio track
|
|
12
|
-
*
|
|
13
|
-
* The caller is responsible for making the HTTP /connect request.
|
|
14
|
-
* Pass the parsed response object to connect() — WebSocketTransport extracts
|
|
15
|
-
* wsUrl/ws_url automatically.
|
|
16
|
-
*/
|
|
17
4
|
export class WebSocketSession extends EventEmitter {
|
|
18
5
|
constructor(onMessage, enableMic = true) {
|
|
19
6
|
super();
|
|
20
7
|
this._isConnected = false;
|
|
8
|
+
this._botAudioMuted = false;
|
|
9
|
+
this._botGain = null;
|
|
10
|
+
this._player = null;
|
|
21
11
|
this._onMessage = onMessage;
|
|
12
|
+
this._transport = new WebSocketTransport();
|
|
22
13
|
this._pcClient = new PipecatClient({
|
|
23
|
-
transport:
|
|
14
|
+
transport: this._transport,
|
|
24
15
|
enableMic,
|
|
25
16
|
enableCam: false,
|
|
26
17
|
callbacks: {
|
|
@@ -38,6 +29,7 @@ export class WebSocketSession extends EventEmitter {
|
|
|
38
29
|
},
|
|
39
30
|
});
|
|
40
31
|
this._setupMessageForwarding();
|
|
32
|
+
this._routePlayerThroughGain();
|
|
41
33
|
}
|
|
42
34
|
get isConnected() {
|
|
43
35
|
return this._isConnected;
|
|
@@ -83,6 +75,58 @@ export class WebSocketSession extends EventEmitter {
|
|
|
83
75
|
this._pcClient.enableMic(enable);
|
|
84
76
|
}
|
|
85
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Silence (or restore) the character's audio for the listener. The
|
|
80
|
+
* character keeps speaking; what it says while muted is dropped, not queued.
|
|
81
|
+
*/
|
|
82
|
+
setBotAudioMuted(muted) {
|
|
83
|
+
this._botAudioMuted = muted;
|
|
84
|
+
if (this._botGain)
|
|
85
|
+
this._botGain.gain.value = muted ? 0 : 1;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Pipecat's WebSocket transport plays the character through its own
|
|
89
|
+
* WavStreamPlayer, wiring each new stream straight to the speakers, and
|
|
90
|
+
* exposes no track to mute. Wrap the player's stream start so every stream
|
|
91
|
+
* goes through a GainNode instead. These are Pipecat internals, so each one
|
|
92
|
+
* is feature-checked: if they are missing, mute degrades to a warning rather
|
|
93
|
+
* than breaking playback.
|
|
94
|
+
*/
|
|
95
|
+
_routePlayerThroughGain() {
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
97
|
+
const player = this._transport?._mediaManager?._wavStreamPlayer;
|
|
98
|
+
if (!player || typeof player._start !== "function") {
|
|
99
|
+
console.warn("[ConvaiClient] This @pipecat-ai/websocket-transport version exposes no audio player to mute; " +
|
|
100
|
+
"setCharacterMuted() will not silence the character on the WebSocket transport.");
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
this._player = player;
|
|
104
|
+
const start = player._start.bind(player);
|
|
105
|
+
player._start = () => {
|
|
106
|
+
const result = start();
|
|
107
|
+
this._attachGain();
|
|
108
|
+
return result;
|
|
109
|
+
};
|
|
110
|
+
this._attachGain();
|
|
111
|
+
}
|
|
112
|
+
_attachGain() {
|
|
113
|
+
const context = this._player?.context;
|
|
114
|
+
const node = this._player?.stream;
|
|
115
|
+
if (!context || !node)
|
|
116
|
+
return;
|
|
117
|
+
if (!this._botGain || this._botGain.context !== context) {
|
|
118
|
+
this._botGain = context.createGain();
|
|
119
|
+
this._botGain.connect(context.destination);
|
|
120
|
+
}
|
|
121
|
+
this._botGain.gain.value = this._botAudioMuted ? 0 : 1;
|
|
122
|
+
try {
|
|
123
|
+
node.disconnect(context.destination);
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
// Not connected to the destination (already routed through the gain).
|
|
127
|
+
}
|
|
128
|
+
node.connect(this._botGain);
|
|
129
|
+
}
|
|
86
130
|
getBotAudioTrack() {
|
|
87
131
|
return this._pcClient.tracks().bot?.audio ?? null;
|
|
88
132
|
}
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "1.9.
|
|
1
|
+
export declare const SDK_VERSION = "1.9.2";
|
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@convai/web-sdk",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.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",
|