@vapi-ai/web 1.0.0 → 1.0.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 +74 -1
- package/dist/player.d.ts +3 -1
- package/dist/player.js +11 -2
- package/dist/vapi.d.ts +4 -2
- package/dist/vapi.js +8 -3
- package/package.json +1 -1
- package/player.ts +14 -2
- package/vapi.ts +11 -4
package/README.md
CHANGED
|
@@ -1 +1,74 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Vapi Web SDK
|
|
2
|
+
|
|
3
|
+
This package lets you start Vapi calls directly in your webapp.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
You can install the package via npm:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @vapi-ai/web
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
First, import the Vapi class from the package:
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
import Vapi from '@vapi-ai/web';
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Then, create a new instance of the Vapi class, passing your Client Token as a parameter to the constructor:
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
const vapi = new Vapi('your-api-token');
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
You can start a new call by calling the `start` method and passing an `agent` object or `agentId`:
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
vapi.start({
|
|
31
|
+
context: "You are a shopping assistant...",
|
|
32
|
+
voice: "steve",
|
|
33
|
+
...
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
```javascript
|
|
37
|
+
vapi.start('your-agent-id');
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
The `start` method will initiate a new call.
|
|
41
|
+
|
|
42
|
+
You can stop the session by calling the `stop` method:
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
vapi.stop();
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
This will stop the recording and close the connection.
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
MIT License
|
|
54
|
+
|
|
55
|
+
Copyright (c) 2023 Vapi Labs Inc.
|
|
56
|
+
|
|
57
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
58
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
59
|
+
in the Software without restriction, including without limitation the rights
|
|
60
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
61
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
62
|
+
furnished to do so, subject to the following conditions:
|
|
63
|
+
|
|
64
|
+
The above copyright notice and this permission notice shall be included in all
|
|
65
|
+
copies or substantial portions of the Software.
|
|
66
|
+
|
|
67
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
68
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
69
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
70
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
71
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
72
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
73
|
+
SOFTWARE.
|
|
74
|
+
```
|
package/dist/player.d.ts
CHANGED
package/dist/player.js
CHANGED
|
@@ -11,15 +11,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.ContinuousPlayer = void 0;
|
|
13
13
|
const async_1 = require("async"); // You need to install the async package
|
|
14
|
-
|
|
14
|
+
const stream_1 = require("stream");
|
|
15
|
+
class ContinuousPlayer extends stream_1.EventEmitter {
|
|
15
16
|
constructor(crossFadeDuration = 0.5) {
|
|
17
|
+
super();
|
|
16
18
|
this.audioCtx = new AudioContext();
|
|
17
19
|
this.nextStartTime = 0;
|
|
18
20
|
this.audioQueue = this.createQueue();
|
|
19
21
|
this.crossFadeDuration = crossFadeDuration;
|
|
20
22
|
}
|
|
21
23
|
createQueue() {
|
|
22
|
-
|
|
24
|
+
const q = (0, async_1.queue)((audioPromise, callback) => __awaiter(this, void 0, void 0, function* () {
|
|
23
25
|
const audioBuffer = yield audioPromise;
|
|
24
26
|
const source = this.audioCtx.createBufferSource();
|
|
25
27
|
source.buffer = audioBuffer;
|
|
@@ -40,6 +42,13 @@ class ContinuousPlayer {
|
|
|
40
42
|
callback();
|
|
41
43
|
};
|
|
42
44
|
}), 1);
|
|
45
|
+
q.drain(() => {
|
|
46
|
+
this.emit("speech-end");
|
|
47
|
+
});
|
|
48
|
+
q.saturated(() => {
|
|
49
|
+
this.emit("speech-start");
|
|
50
|
+
});
|
|
51
|
+
return q;
|
|
43
52
|
}
|
|
44
53
|
playChunk(audioData) {
|
|
45
54
|
const decodePromise = this.audioCtx.decodeAudioData(audioData);
|
package/dist/vapi.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { CreateAgentDTO } from "./api";
|
|
2
|
-
|
|
3
|
+
import { EventEmitter } from "stream";
|
|
4
|
+
export default class Vapi extends EventEmitter {
|
|
3
5
|
private started;
|
|
4
6
|
private ws;
|
|
5
7
|
private mediaRecorder;
|
|
6
8
|
private player;
|
|
7
9
|
constructor(apiToken: string);
|
|
8
|
-
start(agent: CreateAgentDTO): void;
|
|
10
|
+
start(agent: CreateAgentDTO | string): void;
|
|
9
11
|
private startRecording;
|
|
10
12
|
private onMessage;
|
|
11
13
|
clear(): void;
|
package/dist/vapi.js
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const player_1 = require("./player");
|
|
4
|
+
const stream_1 = require("stream");
|
|
4
5
|
const client_1 = require("./client");
|
|
5
6
|
const base64_arraybuffer_1 = require("base64-arraybuffer");
|
|
6
|
-
class Vapi {
|
|
7
|
+
class Vapi extends stream_1.EventEmitter {
|
|
7
8
|
constructor(apiToken) {
|
|
9
|
+
super();
|
|
8
10
|
this.started = false;
|
|
9
11
|
this.ws = null;
|
|
10
12
|
this.mediaRecorder = null;
|
|
11
|
-
this.player = new player_1.ContinuousPlayer();
|
|
12
13
|
client_1.client.setSecurityData(apiToken);
|
|
14
|
+
this.player = new player_1.ContinuousPlayer();
|
|
15
|
+
this.player.on("speech-start", () => this.emit("speech-start"));
|
|
16
|
+
this.player.on("speech-end", () => this.emit("speech-end"));
|
|
13
17
|
}
|
|
14
18
|
start(agent) {
|
|
15
19
|
if (this.started) {
|
|
@@ -18,7 +22,8 @@ class Vapi {
|
|
|
18
22
|
this.started = true;
|
|
19
23
|
client_1.client.call
|
|
20
24
|
.callControllerCreateWebCall({
|
|
21
|
-
agent,
|
|
25
|
+
agent: typeof agent === "string" ? undefined : agent,
|
|
26
|
+
agentId: typeof agent === "string" ? agent : undefined,
|
|
22
27
|
})
|
|
23
28
|
.then(({ data }) => {
|
|
24
29
|
const { callId, url } = data;
|
package/package.json
CHANGED
package/player.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { QueueObject, queue } from "async"; // You need to install the async package
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import { EventEmitter } from "stream";
|
|
4
|
+
|
|
5
|
+
export class ContinuousPlayer extends EventEmitter {
|
|
4
6
|
private audioCtx: AudioContext;
|
|
5
7
|
private nextStartTime: number;
|
|
6
8
|
private audioQueue: QueueObject<Promise<AudioBuffer>>;
|
|
7
9
|
private crossFadeDuration: number; // Duration of the crossfade
|
|
8
10
|
|
|
9
11
|
constructor(crossFadeDuration = 0.5) {
|
|
12
|
+
super();
|
|
10
13
|
this.audioCtx = new AudioContext();
|
|
11
14
|
this.nextStartTime = 0;
|
|
12
15
|
this.audioQueue = this.createQueue();
|
|
@@ -14,7 +17,7 @@ export class ContinuousPlayer {
|
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
private createQueue() {
|
|
17
|
-
|
|
20
|
+
const q = queue(
|
|
18
21
|
async (audioPromise: Promise<AudioBuffer>, callback: Function) => {
|
|
19
22
|
const audioBuffer = await audioPromise;
|
|
20
23
|
const source = this.audioCtx.createBufferSource();
|
|
@@ -51,6 +54,15 @@ export class ContinuousPlayer {
|
|
|
51
54
|
},
|
|
52
55
|
1
|
|
53
56
|
);
|
|
57
|
+
|
|
58
|
+
q.drain(() => {
|
|
59
|
+
this.emit("speech-end");
|
|
60
|
+
});
|
|
61
|
+
q.saturated(() => {
|
|
62
|
+
this.emit("speech-start");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return q;
|
|
54
66
|
}
|
|
55
67
|
|
|
56
68
|
playChunk(audioData: ArrayBuffer) {
|
package/vapi.ts
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
import { Agent, CreateAgentDTO } from "./api";
|
|
2
2
|
|
|
3
3
|
import { ContinuousPlayer } from "./player";
|
|
4
|
+
import { EventEmitter } from "stream";
|
|
4
5
|
import { client } from "./client";
|
|
5
6
|
import { decode } from "base64-arraybuffer";
|
|
6
7
|
|
|
7
|
-
export default class Vapi {
|
|
8
|
+
export default class Vapi extends EventEmitter {
|
|
8
9
|
private started: boolean = false;
|
|
9
10
|
private ws: WebSocket | null = null;
|
|
10
11
|
private mediaRecorder: MediaRecorder | null = null;
|
|
11
|
-
private player
|
|
12
|
+
private player: ContinuousPlayer;
|
|
12
13
|
|
|
13
14
|
constructor(apiToken: string) {
|
|
15
|
+
super();
|
|
14
16
|
client.setSecurityData(apiToken);
|
|
17
|
+
this.player = new ContinuousPlayer();
|
|
18
|
+
|
|
19
|
+
this.player.on("speech-start", () => this.emit("speech-start"));
|
|
20
|
+
this.player.on("speech-end", () => this.emit("speech-end"));
|
|
15
21
|
}
|
|
16
22
|
|
|
17
|
-
start(agent: CreateAgentDTO): void {
|
|
23
|
+
start(agent: CreateAgentDTO | string): void {
|
|
18
24
|
if (this.started) {
|
|
19
25
|
return;
|
|
20
26
|
}
|
|
@@ -23,7 +29,8 @@ export default class Vapi {
|
|
|
23
29
|
|
|
24
30
|
client.call
|
|
25
31
|
.callControllerCreateWebCall({
|
|
26
|
-
agent,
|
|
32
|
+
agent: typeof agent === "string" ? undefined : agent,
|
|
33
|
+
agentId: typeof agent === "string" ? agent : undefined,
|
|
27
34
|
})
|
|
28
35
|
.then(({ data }) => {
|
|
29
36
|
const { callId, url } = data;
|