@convai/web-sdk 1.9.1 → 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 CHANGED
@@ -400,8 +400,8 @@ personal access token; every method rejects with `CharacterApiError` (`status`,
400
400
  non-2xx answer. Point `characterApiUrl` at `https://api2-stg.convai.com` to author against
401
401
  staging.
402
402
 
403
- Explicit selectors are resolved by the runtime through the Character REST platform. Staging has
404
- this today; production answers explicit selectors with `503` until it is promoted there.
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
405
  Full guide: [Character Versioning](https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/character-versioning).
406
406
 
407
407
  ---
@@ -448,7 +448,7 @@ Key points:
448
448
 
449
449
  - `characters` and `characterId` are mutually exclusive, and roster entries take a bare character UUID (no `-draft` / `-1.2` selectors).
450
450
  - `provisioningStatus` describes dispatch only; a member is usable when its own `characterReady` arrives.
451
- - Transcript rows, `characterAudioTrack` events and `botReady` payloads carry `membershipId`, so two copies of one character never merge.
451
+ - Transcript rows, `characterAudioTrack` events and `characterReady` payloads carry `membershipId`, so two copies of one character never merge. (`botReady` takes no arguments.)
452
452
  - Room-topology failures throw `ConvaiRoomError` with `status`, `code`, `requestTraceId` and `isRetryable`; the SDK retries `ROSTER_PROVISIONING_IN_PROGRESS` for you (`rosterProvisioningRetry`).
453
453
  - Action protocol v2 and `actionConfig.tools` are single-character only and cannot be combined with a roster.
454
454
 
@@ -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
- const wsUrl = connectionData.room_url;
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) {
@@ -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)`, `![alt](url)`
924
930
  * and bare `http(s)://` URLs verbatim (trailing sentence punctuation stays
925
- * outside the URL, non-http(s) schemes are still stripped) while bold,
926
- * headings, tables and code are flattened as before. Speech is unaffected —
927
- * URLs are never read aloud.
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)`, `![alt](url)` and
6
- * bare `http(s)://` URLs arrive verbatim, everything else is still flattened
7
- * server-side. This turns one line of that text into nodes a renderer can
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 `http:` and `https:` become links or images. Anything else — including
11
- * `javascript:` — stays literal text, regardless of what the server sent.
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)`, `![alt](url)` and
6
- * bare `http(s)://` URLs arrive verbatim, everything else is still flattened
7
- * server-side. This turns one line of that text into nodes a renderer can
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 `http:` and `https:` become links or images. Anything else — including
11
- * `javascript:` — stays literal text, regardless of what the server sent.
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: new WebSocketTransport(),
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";
1
+ export declare const SDK_VERSION = "1.9.2";
package/dist/version.js CHANGED
@@ -1,3 +1,3 @@
1
1
  // GENERATED by scripts/sync-version.mjs — do not edit by hand.
2
2
  // Regenerated from package.json on every build.
3
- export const SDK_VERSION = "1.9.1";
3
+ export const SDK_VERSION = "1.9.2";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@convai/web-sdk",
3
- "version": "1.9.1",
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",