@craftedxp/voice-js 0.6.0 → 0.9.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/dist/browser.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,13 +17,21 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/browser.ts
21
31
  var browser_exports = {};
22
32
  __export(browser_exports, {
23
33
  buildWsUrl: () => buildWsUrl,
24
- configureVoiceClient: () => configureVoiceClient,
34
+ configureVoiceClient: () => configureVoiceClient2,
25
35
  createAudioCapture: () => createAudioCapture,
26
36
  createAudioPlayback: () => createAudioPlayback,
27
37
  createProtocolState: () => createProtocolState,
@@ -1050,177 +1060,6 @@ async function createWebRtcCall(opts) {
1050
1060
  };
1051
1061
  }
1052
1062
 
1053
- // src/room.ts
1054
- var import_livekit_client = require("livekit-client");
1055
-
1056
- // src/roomProtocol.ts
1057
- var SYSTEM_TOPIC = "system";
1058
- var TRANSCRIPT_TOPIC = "transcript";
1059
- var decodeSystem = (bytes) => {
1060
- try {
1061
- const v = JSON.parse(new TextDecoder().decode(bytes));
1062
- if (v && typeof v.kind === "string") return v;
1063
- return null;
1064
- } catch {
1065
- return null;
1066
- }
1067
- };
1068
- var decodeTranscript = (bytes) => {
1069
- try {
1070
- const v = JSON.parse(new TextDecoder().decode(bytes));
1071
- if (v && v.kind === "partial") return v;
1072
- return null;
1073
- } catch {
1074
- return null;
1075
- }
1076
- };
1077
-
1078
- // src/room.ts
1079
- var identityToPid = (identity) => identity.startsWith("guest:") ? identity.slice("guest:".length) : identity;
1080
- var joinRoom = async (opts) => {
1081
- const exchangeUrl = `${opts.apiBase.replace(/\/+$/, "")}/v1/rooms/${encodeURIComponent(
1082
- opts.roomId
1083
- )}/join`;
1084
- const exchangeRes = await fetch(exchangeUrl, {
1085
- method: "POST",
1086
- headers: { "Content-Type": "application/json" },
1087
- body: JSON.stringify({ code: opts.joinCode, name: opts.name })
1088
- });
1089
- if (!exchangeRes.ok) {
1090
- const err = await exchangeRes.json().catch(() => ({}));
1091
- throw new Error(err.error?.code ?? `join_failed_${exchangeRes.status}`);
1092
- }
1093
- const exchange = await exchangeRes.json();
1094
- const handlers = /* @__PURE__ */ new Map();
1095
- const emit = (e, payload) => {
1096
- handlers.get(e)?.forEach((h) => {
1097
- try {
1098
- h(payload);
1099
- } catch {
1100
- }
1101
- });
1102
- };
1103
- const room = new import_livekit_client.Room({ adaptiveStream: true, dynacast: true });
1104
- room.on(
1105
- import_livekit_client.RoomEvent.ParticipantConnected,
1106
- (p) => emit("participant.joined", {
1107
- participantId: identityToPid(p.identity),
1108
- name: p.name ?? ""
1109
- })
1110
- );
1111
- room.on(
1112
- import_livekit_client.RoomEvent.ParticipantDisconnected,
1113
- (p) => emit("participant.left", {
1114
- participantId: identityToPid(p.identity),
1115
- name: p.name ?? ""
1116
- })
1117
- );
1118
- room.on(import_livekit_client.RoomEvent.Disconnected, () => emit("room.ended", void 0));
1119
- room.on(import_livekit_client.RoomEvent.DataReceived, (data, _participant, _kind, topic) => {
1120
- if (topic === SYSTEM_TOPIC) {
1121
- const m = decodeSystem(data);
1122
- if (m) emit("system.message", m);
1123
- } else if (topic === TRANSCRIPT_TOPIC) {
1124
- const m = decodeTranscript(data);
1125
- if (m) emit("transcript.partial", m);
1126
- }
1127
- });
1128
- const trackKind = (t) => t.kind === import_livekit_client.Track.Kind.Video ? "video" : "audio";
1129
- const trackSource = (s) => {
1130
- switch (s) {
1131
- case import_livekit_client.Track.Source.Camera:
1132
- return "camera";
1133
- case import_livekit_client.Track.Source.Microphone:
1134
- return "microphone";
1135
- case import_livekit_client.Track.Source.ScreenShare:
1136
- return "screen_share";
1137
- case import_livekit_client.Track.Source.ScreenShareAudio:
1138
- return "screen_share_audio";
1139
- default:
1140
- return "unknown";
1141
- }
1142
- };
1143
- room.on(
1144
- import_livekit_client.RoomEvent.TrackSubscribed,
1145
- (track, pub, participant) => emit("track.subscribed", {
1146
- participantId: identityToPid(participant.identity),
1147
- kind: trackKind(track),
1148
- source: trackSource(pub.source),
1149
- track
1150
- })
1151
- );
1152
- room.on(
1153
- import_livekit_client.RoomEvent.TrackUnsubscribed,
1154
- (track, pub, participant) => emit("track.unsubscribed", {
1155
- participantId: identityToPid(participant.identity),
1156
- kind: trackKind(track),
1157
- source: trackSource(pub.source),
1158
- track
1159
- })
1160
- );
1161
- room.on(
1162
- import_livekit_client.RoomEvent.ActiveSpeakersChanged,
1163
- (speakers) => emit(
1164
- "active.speakers",
1165
- speakers.map((p) => identityToPid(p.identity))
1166
- )
1167
- );
1168
- await room.connect(exchange.livekit.url, exchange.livekit.token);
1169
- return {
1170
- participantId: exchange.participantId,
1171
- get participants() {
1172
- return [...room.remoteParticipants.values()].map((p) => ({
1173
- participantId: identityToPid(p.identity),
1174
- name: p.name ?? ""
1175
- }));
1176
- },
1177
- on(event, handler) {
1178
- const set = handlers.get(event) ?? /* @__PURE__ */ new Set();
1179
- set.add(handler);
1180
- handlers.set(event, set);
1181
- },
1182
- publishMic: async () => {
1183
- await room.localParticipant.setMicrophoneEnabled(true);
1184
- },
1185
- publishCamera: async () => {
1186
- await room.localParticipant.setCameraEnabled(true);
1187
- },
1188
- setMicEnabled: async (on) => {
1189
- await room.localParticipant.setMicrophoneEnabled(on);
1190
- },
1191
- setCameraEnabled: async (on) => {
1192
- await room.localParticipant.setCameraEnabled(on);
1193
- },
1194
- isMicEnabled: () => room.localParticipant.isMicrophoneEnabled,
1195
- isCameraEnabled: () => room.localParticipant.isCameraEnabled,
1196
- getLocalCameraTrack: () => room.localParticipant.getTrackPublication(import_livekit_client.Track.Source.Camera)?.videoTrack ?? null,
1197
- getRemoteTracks: () => {
1198
- const out = [];
1199
- for (const p of room.remoteParticipants.values()) {
1200
- for (const pub of p.trackPublications.values()) {
1201
- const track = pub.track;
1202
- if (!track) continue;
1203
- out.push({
1204
- participantId: identityToPid(p.identity),
1205
- kind: trackKind(track),
1206
- source: trackSource(pub.source),
1207
- track
1208
- });
1209
- }
1210
- }
1211
- return out;
1212
- },
1213
- setScreenShareEnabled: async (on, opts2) => {
1214
- await room.localParticipant.setScreenShareEnabled(on, { audio: opts2?.audio ?? false });
1215
- },
1216
- isScreenShareEnabled: () => room.localParticipant.isScreenShareEnabled,
1217
- getLocalScreenTrack: () => room.localParticipant.getTrackPublication(import_livekit_client.Track.Source.ScreenShare)?.videoTrack ?? null,
1218
- leave: async () => {
1219
- await room.disconnect();
1220
- }
1221
- };
1222
- };
1223
-
1224
1063
  // src/textSession.ts
1225
1064
  async function startTextSession(opts) {
1226
1065
  const f = opts.fetch ?? fetch;
@@ -1328,9 +1167,9 @@ var parseIncomingCall = (raw) => {
1328
1167
  return out;
1329
1168
  };
1330
1169
 
1331
- // src/browser.ts
1170
+ // src/assistant.ts
1332
1171
  var browserWsFactory = (url) => new globalThis.WebSocket(url);
1333
- var BrowserVoiceFactory = class {
1172
+ var AssistantVoiceFactory = class {
1334
1173
  constructor(config) {
1335
1174
  this.startCall = async (options) => {
1336
1175
  if (!options.agentId) {
@@ -1385,14 +1224,6 @@ var BrowserVoiceFactory = class {
1385
1224
  await client.start();
1386
1225
  return client;
1387
1226
  };
1388
- // Multi-party rooms (Phase 7 video).
1389
- //
1390
- // The guest's browser calls this with the roomId + joinCode it parsed
1391
- // out of the invite link. The SDK exchanges the code for a LiveKit
1392
- // JWT against `${apiBase}/v1/rooms/:roomId/join` (an AUTH-EXEMPT
1393
- // endpoint — the opaque code is the only credential), then connects
1394
- // to LiveKit and returns a typed event surface.
1395
- this.joinRoom = (opts) => joinRoom({ apiBase: this.config.apiBase, ...opts });
1396
1227
  // Text-channel chat session (no microphone / audio).
1397
1228
  // Mint a `ct_` token with `channel: 'text'` on your backend, then call
1398
1229
  // this to open an SSE stream against the chat API.
@@ -1404,7 +1235,215 @@ var BrowserVoiceFactory = class {
1404
1235
  }
1405
1236
  };
1406
1237
  function configureVoiceClient(config) {
1407
- return new BrowserVoiceFactory(normalizeConfig(config));
1238
+ return new AssistantVoiceFactory(normalizeConfig(config));
1239
+ }
1240
+
1241
+ // src/roomProtocol.ts
1242
+ var SYSTEM_TOPIC = "system";
1243
+ var TRANSCRIPT_TOPIC = "transcript";
1244
+ var ANALYSIS_TOPIC = "analysis";
1245
+ var decodeSystem = (bytes) => {
1246
+ try {
1247
+ const v = JSON.parse(new TextDecoder().decode(bytes));
1248
+ if (v && typeof v.kind === "string") return v;
1249
+ return null;
1250
+ } catch {
1251
+ return null;
1252
+ }
1253
+ };
1254
+ var decodeTranscript = (bytes) => {
1255
+ try {
1256
+ const v = JSON.parse(new TextDecoder().decode(bytes));
1257
+ if (v && v.kind === "partial") return v;
1258
+ return null;
1259
+ } catch {
1260
+ return null;
1261
+ }
1262
+ };
1263
+ var decodeAnalysis = (bytes) => {
1264
+ try {
1265
+ const v = JSON.parse(new TextDecoder().decode(bytes));
1266
+ if (v && v.kind === "analysis" && typeof v.participantId === "string" && typeof v.label === "string") {
1267
+ return v;
1268
+ }
1269
+ return null;
1270
+ } catch {
1271
+ return null;
1272
+ }
1273
+ };
1274
+
1275
+ // src/room.ts
1276
+ var lkPromise;
1277
+ var loadLiveKit = () => {
1278
+ lkPromise ?? (lkPromise = import("livekit-client").catch((e) => {
1279
+ lkPromise = void 0;
1280
+ const msg = "joinRoom requires the optional peer dependency 'livekit-client' \u2014 npm install livekit-client";
1281
+ const wrapped = new Error(msg);
1282
+ wrapped.cause = e;
1283
+ throw wrapped;
1284
+ }));
1285
+ return lkPromise;
1286
+ };
1287
+ var identityToPid = (identity) => identity.startsWith("guest:") ? identity.slice("guest:".length) : identity;
1288
+ var joinRoom = async (opts) => {
1289
+ const exchangeUrl = `${opts.apiBase.replace(/\/+$/, "")}/v1/rooms/${encodeURIComponent(
1290
+ opts.roomId
1291
+ )}/join`;
1292
+ const exchangeRes = await fetch(exchangeUrl, {
1293
+ method: "POST",
1294
+ headers: { "Content-Type": "application/json" },
1295
+ body: JSON.stringify({ code: opts.joinCode, name: opts.name })
1296
+ });
1297
+ if (!exchangeRes.ok) {
1298
+ const err = await exchangeRes.json().catch(() => ({}));
1299
+ throw new Error(err.error?.code ?? `join_failed_${exchangeRes.status}`);
1300
+ }
1301
+ const exchange = await exchangeRes.json();
1302
+ const handlers = /* @__PURE__ */ new Map();
1303
+ const emit = (e, payload) => {
1304
+ handlers.get(e)?.forEach((h) => {
1305
+ try {
1306
+ h(payload);
1307
+ } catch {
1308
+ }
1309
+ });
1310
+ };
1311
+ const lk = await loadLiveKit();
1312
+ const trackKind = (t) => t.kind === lk.Track.Kind.Video ? "video" : "audio";
1313
+ const trackSource = (s) => {
1314
+ switch (s) {
1315
+ case lk.Track.Source.Camera:
1316
+ return "camera";
1317
+ case lk.Track.Source.Microphone:
1318
+ return "microphone";
1319
+ case lk.Track.Source.ScreenShare:
1320
+ return "screen_share";
1321
+ case lk.Track.Source.ScreenShareAudio:
1322
+ return "screen_share_audio";
1323
+ default:
1324
+ return "unknown";
1325
+ }
1326
+ };
1327
+ const room = new lk.Room({ adaptiveStream: true, dynacast: true });
1328
+ room.on(
1329
+ lk.RoomEvent.ParticipantConnected,
1330
+ (p) => emit("participant.joined", {
1331
+ participantId: identityToPid(p.identity),
1332
+ name: p.name ?? ""
1333
+ })
1334
+ );
1335
+ room.on(
1336
+ lk.RoomEvent.ParticipantDisconnected,
1337
+ (p) => emit("participant.left", {
1338
+ participantId: identityToPid(p.identity),
1339
+ name: p.name ?? ""
1340
+ })
1341
+ );
1342
+ room.on(lk.RoomEvent.Disconnected, () => emit("room.ended", void 0));
1343
+ room.on(lk.RoomEvent.DataReceived, (data, _participant, _kind, topic) => {
1344
+ if (topic === SYSTEM_TOPIC) {
1345
+ const m = decodeSystem(data);
1346
+ if (m) emit("system.message", m);
1347
+ } else if (topic === TRANSCRIPT_TOPIC) {
1348
+ const m = decodeTranscript(data);
1349
+ if (m) emit("transcript.partial", m);
1350
+ } else if (topic === ANALYSIS_TOPIC) {
1351
+ const m = decodeAnalysis(data);
1352
+ if (m) emit("analysis", m);
1353
+ }
1354
+ });
1355
+ room.on(
1356
+ lk.RoomEvent.TrackSubscribed,
1357
+ (track, pub, participant) => emit("track.subscribed", {
1358
+ participantId: identityToPid(participant.identity),
1359
+ kind: trackKind(track),
1360
+ source: trackSource(pub.source),
1361
+ track
1362
+ })
1363
+ );
1364
+ room.on(
1365
+ lk.RoomEvent.TrackUnsubscribed,
1366
+ (track, pub, participant) => emit("track.unsubscribed", {
1367
+ participantId: identityToPid(participant.identity),
1368
+ kind: trackKind(track),
1369
+ source: trackSource(pub.source),
1370
+ track
1371
+ })
1372
+ );
1373
+ room.on(
1374
+ lk.RoomEvent.ActiveSpeakersChanged,
1375
+ (speakers) => emit(
1376
+ "active.speakers",
1377
+ speakers.map((p) => identityToPid(p.identity))
1378
+ )
1379
+ );
1380
+ await room.connect(exchange.livekit.url, exchange.livekit.token);
1381
+ return {
1382
+ participantId: exchange.participantId,
1383
+ kind: exchange.kind ?? "video",
1384
+ get participants() {
1385
+ return [...room.remoteParticipants.values()].map((p) => ({
1386
+ participantId: identityToPid(p.identity),
1387
+ name: p.name ?? ""
1388
+ }));
1389
+ },
1390
+ on(event, handler) {
1391
+ const set = handlers.get(event) ?? /* @__PURE__ */ new Set();
1392
+ set.add(handler);
1393
+ handlers.set(event, set);
1394
+ },
1395
+ publishMic: async () => {
1396
+ await room.localParticipant.setMicrophoneEnabled(true);
1397
+ },
1398
+ publishCamera: async () => {
1399
+ await room.localParticipant.setCameraEnabled(true);
1400
+ },
1401
+ setMicEnabled: async (on) => {
1402
+ await room.localParticipant.setMicrophoneEnabled(on);
1403
+ },
1404
+ setCameraEnabled: async (on) => {
1405
+ await room.localParticipant.setCameraEnabled(on);
1406
+ },
1407
+ isMicEnabled: () => room.localParticipant.isMicrophoneEnabled,
1408
+ isCameraEnabled: () => room.localParticipant.isCameraEnabled,
1409
+ getLocalCameraTrack: () => room.localParticipant.getTrackPublication(lk.Track.Source.Camera)?.videoTrack ?? null,
1410
+ getRemoteTracks: () => {
1411
+ const out = [];
1412
+ for (const p of room.remoteParticipants.values()) {
1413
+ for (const pub of p.trackPublications.values()) {
1414
+ const track = pub.track;
1415
+ if (!track) continue;
1416
+ out.push({
1417
+ participantId: identityToPid(p.identity),
1418
+ kind: trackKind(track),
1419
+ source: trackSource(pub.source),
1420
+ track
1421
+ });
1422
+ }
1423
+ }
1424
+ return out;
1425
+ },
1426
+ setScreenShareEnabled: async (on, opts2) => {
1427
+ await room.localParticipant.setScreenShareEnabled(on, { audio: opts2?.audio ?? false });
1428
+ },
1429
+ isScreenShareEnabled: () => room.localParticipant.isScreenShareEnabled,
1430
+ getLocalScreenTrack: () => room.localParticipant.getTrackPublication(lk.Track.Source.ScreenShare)?.videoTrack ?? null,
1431
+ leave: async () => {
1432
+ await room.disconnect();
1433
+ }
1434
+ };
1435
+ };
1436
+
1437
+ // src/browser.ts
1438
+ function configureVoiceClient2(config) {
1439
+ const normalized = normalizeConfig(config);
1440
+ const factory = configureVoiceClient(normalized);
1441
+ return {
1442
+ ...factory,
1443
+ // Re-compose joinRoom onto the factory so barrel consumers who call
1444
+ // `voice.joinRoom(...)` keep working unchanged.
1445
+ joinRoom: (opts) => joinRoom({ apiBase: normalized.apiBase, ...opts })
1446
+ };
1408
1447
  }
1409
1448
  // Annotate the CommonJS export names for ESM import in node:
1410
1449
  0 && (module.exports = {