@vapi-ai/web 1.0.191 → 1.0.193

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.
Files changed (3) hide show
  1. package/dist/vapi.js +41 -9
  2. package/package.json +1 -1
  3. package/vapi.ts +37 -4
package/dist/vapi.js CHANGED
@@ -15,6 +15,30 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const daily_js_1 = __importDefault(require("@daily-co/daily-js"));
16
16
  const events_1 = __importDefault(require("events"));
17
17
  const client_1 = require("./client");
18
+ function destroyAudioPlayer(participantId) {
19
+ const player = document.querySelector(`audio[data-participant-id="${participantId}"]`);
20
+ player === null || player === void 0 ? void 0 : player.remove();
21
+ }
22
+ function startPlayer(player, track) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ player.muted = false;
25
+ player.autoplay = true;
26
+ if (track != null) {
27
+ player.srcObject = new MediaStream([track]);
28
+ yield player.play();
29
+ }
30
+ });
31
+ }
32
+ function buildAudioPlayer(track, participantId) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ const audioContainer = document.getElementById("audio-container");
35
+ const player = document.createElement("audio");
36
+ player.dataset.participantId = participantId;
37
+ audioContainer === null || audioContainer === void 0 ? void 0 : audioContainer.appendChild(player);
38
+ yield startPlayer(player, track);
39
+ return player;
40
+ });
41
+ }
18
42
  class Vapi extends events_1.default {
19
43
  constructor(apiToken, apiBaseUrl) {
20
44
  super();
@@ -37,7 +61,7 @@ class Vapi extends events_1.default {
37
61
  .then(({ data }) => __awaiter(this, void 0, void 0, function* () {
38
62
  var _a;
39
63
  const { url } = data;
40
- this.call = daily_js_1.default.createFrame({
64
+ this.call = daily_js_1.default.createCallObject({
41
65
  audioSource: true,
42
66
  videoSource: false,
43
67
  });
@@ -45,25 +69,33 @@ class Vapi extends events_1.default {
45
69
  this.call.on("left-meeting", () => {
46
70
  this.emit("call-end");
47
71
  });
72
+ this.call.on("participant-left", (e) => {
73
+ if (!e)
74
+ return;
75
+ destroyAudioPlayer(e.participant.session_id);
76
+ });
48
77
  this.call.on("error", () => {
49
78
  // Don't print to console
50
79
  });
51
- this.call.on("track-started", (e) => {
52
- var _a, _b;
80
+ this.call.on("track-started", (e) => __awaiter(this, void 0, void 0, function* () {
81
+ var _b, _c;
53
82
  console.log("TRACK");
54
83
  console.log(e);
55
- if (((_a = e === null || e === void 0 ? void 0 : e.participant) === null || _a === void 0 ? void 0 : _a.user_name) !== "Vapi Speaker")
84
+ if (!e)
56
85
  return;
57
- (_b = this.call) === null || _b === void 0 ? void 0 : _b.sendAppMessage("playable");
86
+ if (e.track.kind === "audio" && e.participant) {
87
+ yield buildAudioPlayer(e.track, e.participant.session_id);
88
+ }
89
+ if (((_b = e === null || e === void 0 ? void 0 : e.participant) === null || _b === void 0 ? void 0 : _b.user_name) !== "Vapi Speaker")
90
+ return;
91
+ (_c = this.call) === null || _c === void 0 ? void 0 : _c.sendAppMessage("playable");
58
92
  this.emit("call-start");
59
- });
93
+ }));
60
94
  this.call.on("participant-joined", (e) => {
61
95
  var _a, _b;
62
96
  console.log("JOINED");
63
97
  console.log(e);
64
- if ((e === null || e === void 0 ? void 0 : e.participant.user_name) !== "Vapi Speaker")
65
- return;
66
- (_a = this.call) === null || _a === void 0 ? void 0 : _a.updateParticipant((_b = e === null || e === void 0 ? void 0 : e.participant.user_id) !== null && _b !== void 0 ? _b : "", {
98
+ (_a = this.call) === null || _a === void 0 ? void 0 : _a.updateParticipant((_b = e === null || e === void 0 ? void 0 : e.participant.session_id) !== null && _b !== void 0 ? _b : "", {
67
99
  setSubscribedTracks: {
68
100
  audio: true,
69
101
  video: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vapi-ai/web",
3
- "version": "1.0.191",
3
+ "version": "1.0.193",
4
4
  "description": "",
5
5
  "main": "dist/vapi.js",
6
6
  "types": "dist/vapi.d.ts",
package/vapi.ts CHANGED
@@ -9,6 +9,29 @@ import { CreateAssistantDTO } from "./api";
9
9
  import EventEmitter from "events";
10
10
  import { client } from "./client";
11
11
 
12
+ function destroyAudioPlayer(participantId: string) {
13
+ const player = document.querySelector(
14
+ `audio[data-participant-id="${participantId}"]`
15
+ );
16
+ player?.remove();
17
+ }
18
+ async function startPlayer(player: HTMLAudioElement, track: any) {
19
+ player.muted = false;
20
+ player.autoplay = true;
21
+ if (track != null) {
22
+ player.srcObject = new MediaStream([track]);
23
+ await player.play();
24
+ }
25
+ }
26
+ async function buildAudioPlayer(track: any, participantId: string) {
27
+ const audioContainer = document.getElementById("audio-container");
28
+ const player = document.createElement("audio");
29
+ player.dataset.participantId = participantId;
30
+ audioContainer?.appendChild(player);
31
+ await startPlayer(player, track);
32
+ return player;
33
+ }
34
+
12
35
  export default class Vapi extends EventEmitter {
13
36
  private started: boolean = false;
14
37
  private call: DailyCall | null = null;
@@ -35,7 +58,7 @@ export default class Vapi extends EventEmitter {
35
58
  .then(async ({ data }) => {
36
59
  const { url } = data;
37
60
 
38
- this.call = DailyIframe.createFrame({
61
+ this.call = DailyIframe.createCallObject({
39
62
  audioSource: true,
40
63
  videoSource: false,
41
64
  });
@@ -45,13 +68,24 @@ export default class Vapi extends EventEmitter {
45
68
  this.emit("call-end");
46
69
  });
47
70
 
71
+ this.call.on("participant-left", (e) => {
72
+ if (!e) return;
73
+ destroyAudioPlayer(e.participant.session_id);
74
+ });
75
+
48
76
  this.call.on("error", () => {
49
77
  // Don't print to console
50
78
  });
51
79
 
52
- this.call.on("track-started", (e) => {
80
+ this.call.on("track-started", async (e) => {
53
81
  console.log("TRACK");
54
82
  console.log(e);
83
+
84
+ if (!e) return;
85
+ if (e.track.kind === "audio" && e.participant) {
86
+ await buildAudioPlayer(e.track, e.participant.session_id);
87
+ }
88
+
55
89
  if (e?.participant?.user_name !== "Vapi Speaker") return;
56
90
  this.call?.sendAppMessage("playable");
57
91
  this.emit("call-start");
@@ -60,9 +94,8 @@ export default class Vapi extends EventEmitter {
60
94
  this.call.on("participant-joined", (e) => {
61
95
  console.log("JOINED");
62
96
  console.log(e);
63
- if (e?.participant.user_name !== "Vapi Speaker") return;
64
97
 
65
- this.call?.updateParticipant(e?.participant.user_id ?? "", {
98
+ this.call?.updateParticipant(e?.participant.session_id ?? "", {
66
99
  setSubscribedTracks: {
67
100
  audio: true,
68
101
  video: false,