@vapi-ai/web 1.0.14 → 1.0.15

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/player.d.ts CHANGED
@@ -4,8 +4,10 @@ export declare class ContinuousPlayer extends EventEmitter {
4
4
  private mediaSource;
5
5
  private sourceBuffer;
6
6
  private audio;
7
+ private audioQueue;
7
8
  constructor();
8
9
  start(): void;
9
10
  playChunk(audioData: ArrayBuffer): void;
10
- clear(): Promise<void>;
11
+ private appendNextChunk;
12
+ clear(): void;
11
13
  }
package/dist/player.js CHANGED
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -19,33 +10,42 @@ class ContinuousPlayer extends events_1.default {
19
10
  super();
20
11
  this.sourceBuffer = null;
21
12
  this.audio = null;
13
+ this.audioQueue = [];
22
14
  this.mediaSource = new MediaSource();
23
- this.audio = new Audio();
15
+ this.audio = document.createElement("audio");
24
16
  this.audio.src = URL.createObjectURL(this.mediaSource);
25
- console.log("init");
26
- this.mediaSource.onsourceopen = () => {
27
- console.log("source open");
17
+ this.mediaSource.addEventListener("sourceopen", () => {
28
18
  this.sourceBuffer = this.mediaSource.addSourceBuffer('audio/webm; codecs="opus"');
29
- };
19
+ this.sourceBuffer.addEventListener("updateend", () => {
20
+ this.appendNextChunk();
21
+ });
22
+ });
30
23
  }
31
24
  start() {
32
25
  var _a;
33
26
  (_a = this.audio) === null || _a === void 0 ? void 0 : _a.play();
34
27
  }
35
28
  playChunk(audioData) {
36
- if (!this.sourceBuffer)
37
- return;
38
- console.log(this.mediaSource.readyState, this.sourceBuffer.updating);
39
- if (this.mediaSource.readyState === "open" && !this.sourceBuffer.updating) {
40
- this.sourceBuffer.appendBuffer(audioData);
29
+ this.audioQueue.push(audioData);
30
+ this.appendNextChunk();
31
+ }
32
+ appendNextChunk() {
33
+ if (this.sourceBuffer &&
34
+ !this.sourceBuffer.updating &&
35
+ this.audioQueue.length > 0) {
36
+ const chunk = this.audioQueue.shift();
37
+ if (chunk) {
38
+ this.sourceBuffer.appendBuffer(chunk);
39
+ }
41
40
  }
42
41
  }
43
42
  clear() {
44
- return __awaiter(this, void 0, void 0, function* () {
45
- if (this.sourceBuffer && this.mediaSource.readyState === "open") {
43
+ if (this.sourceBuffer) {
44
+ this.sourceBuffer.abort();
45
+ while (this.sourceBuffer.buffered.length > 0) {
46
46
  this.sourceBuffer.remove(0, this.sourceBuffer.buffered.end(0));
47
47
  }
48
- });
48
+ }
49
49
  }
50
50
  }
51
51
  exports.ContinuousPlayer = ContinuousPlayer;
package/dist/vapi.d.ts CHANGED
@@ -6,6 +6,7 @@ export default class Vapi extends EventEmitter {
6
6
  private ws;
7
7
  private mediaRecorder;
8
8
  private player;
9
+ private audioQueue;
9
10
  constructor(apiToken: string);
10
11
  start(agent: CreateAgentDTO | string): void;
11
12
  private startRecording;
package/dist/vapi.js CHANGED
@@ -13,6 +13,7 @@ class Vapi extends events_1.default {
13
13
  this.started = false;
14
14
  this.ws = null;
15
15
  this.mediaRecorder = null;
16
+ this.audioQueue = [];
16
17
  client_1.client.setSecurityData(apiToken);
17
18
  this.player = new player_1.ContinuousPlayer();
18
19
  this.player.on("speech-start", () => this.emit("speech-start"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vapi-ai/web",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "description": "",
5
5
  "main": "dist/vapi.js",
6
6
  "types": "dist/vapi.d.ts",
package/player.ts CHANGED
@@ -4,19 +4,22 @@ export class ContinuousPlayer extends EventEmitter {
4
4
  private mediaSource: MediaSource;
5
5
  private sourceBuffer: SourceBuffer | null = null;
6
6
  private audio: HTMLAudioElement | null = null;
7
+ private audioQueue: ArrayBuffer[] = [];
7
8
 
8
9
  constructor() {
9
10
  super();
10
11
  this.mediaSource = new MediaSource();
11
- this.audio = new Audio();
12
+ this.audio = document.createElement("audio");
12
13
  this.audio.src = URL.createObjectURL(this.mediaSource);
13
- console.log("init");
14
- this.mediaSource.onsourceopen = () => {
15
- console.log("source open");
14
+
15
+ this.mediaSource.addEventListener("sourceopen", () => {
16
16
  this.sourceBuffer = this.mediaSource.addSourceBuffer(
17
17
  'audio/webm; codecs="opus"'
18
18
  );
19
- };
19
+ this.sourceBuffer.addEventListener("updateend", () => {
20
+ this.appendNextChunk();
21
+ });
22
+ });
20
23
  }
21
24
 
22
25
  start() {
@@ -24,16 +27,30 @@ export class ContinuousPlayer extends EventEmitter {
24
27
  }
25
28
 
26
29
  playChunk(audioData: ArrayBuffer) {
27
- if (!this.sourceBuffer) return;
28
- console.log(this.mediaSource.readyState, this.sourceBuffer.updating);
29
- if (this.mediaSource.readyState === "open" && !this.sourceBuffer.updating) {
30
- this.sourceBuffer.appendBuffer(audioData);
30
+ this.audioQueue.push(audioData);
31
+
32
+ this.appendNextChunk();
33
+ }
34
+
35
+ private appendNextChunk(): void {
36
+ if (
37
+ this.sourceBuffer &&
38
+ !this.sourceBuffer.updating &&
39
+ this.audioQueue.length > 0
40
+ ) {
41
+ const chunk = this.audioQueue.shift();
42
+ if (chunk) {
43
+ this.sourceBuffer.appendBuffer(chunk);
44
+ }
31
45
  }
32
46
  }
33
47
 
34
- async clear() {
35
- if (this.sourceBuffer && this.mediaSource.readyState === "open") {
36
- this.sourceBuffer.remove(0, this.sourceBuffer.buffered.end(0));
48
+ clear(): void {
49
+ if (this.sourceBuffer) {
50
+ this.sourceBuffer.abort();
51
+ while (this.sourceBuffer.buffered.length > 0) {
52
+ this.sourceBuffer.remove(0, this.sourceBuffer.buffered.end(0));
53
+ }
37
54
  }
38
55
  }
39
56
  }
package/vapi.ts CHANGED
@@ -10,6 +10,7 @@ export default class Vapi extends EventEmitter {
10
10
  private ws: WebSocket | null = null;
11
11
  private mediaRecorder: MediaRecorder | null = null;
12
12
  private player: ContinuousPlayer;
13
+ private audioQueue: ArrayBuffer[] = [];
13
14
 
14
15
  constructor(apiToken: string) {
15
16
  super();