@vapi-ai/web 1.0.257 → 1.0.258
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/__tests__/vapi.test.ts +53 -0
- package/jest.config.ts +11 -0
- package/package.json +7 -3
- package/vapi.ts +37 -5
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import Vapi from '../vapi'; // Adjust the import path based on your project structure
|
|
2
|
+
|
|
3
|
+
describe('Vapi', () => {
|
|
4
|
+
let vapi: Vapi;
|
|
5
|
+
let mockCall: any;
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
// Mock the DailyCall object
|
|
9
|
+
mockCall = {
|
|
10
|
+
setLocalAudio: jest.fn(),
|
|
11
|
+
localAudio: jest.fn(),
|
|
12
|
+
};
|
|
13
|
+
// Initialize Vapi instance and inject the mock
|
|
14
|
+
vapi = new Vapi('dummy_token');
|
|
15
|
+
vapi['call'] = mockCall; // Assuming you have a way to set this for testing
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe('setMuted', () => {
|
|
19
|
+
it('should mute the audio', () => {
|
|
20
|
+
vapi['setMuted'](true);
|
|
21
|
+
expect(mockCall.setLocalAudio).toHaveBeenCalledWith(false);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should unmute the audio', () => {
|
|
25
|
+
vapi['setMuted'](false);
|
|
26
|
+
expect(mockCall.setLocalAudio).toHaveBeenCalledWith(true);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should handle errors when call object is not available', () => {
|
|
30
|
+
vapi['call'] = null; // Simulate call object not being available
|
|
31
|
+
expect(() => vapi['setMuted'](true)).toThrow('Call object is not available.');
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('toggleMute', () => {
|
|
36
|
+
it('should toggle audio from unmuted to muted', () => {
|
|
37
|
+
mockCall.localAudio.mockReturnValue(true); // Initially not muted
|
|
38
|
+
vapi.toggleMute();
|
|
39
|
+
expect(mockCall.setLocalAudio).toHaveBeenCalledWith(false); // Should mute
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should toggle audio from muted to unmuted', () => {
|
|
43
|
+
mockCall.localAudio.mockReturnValue(false); // Initially muted
|
|
44
|
+
vapi.toggleMute();
|
|
45
|
+
expect(mockCall.setLocalAudio).toHaveBeenCalledWith(true); // Should unmute
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should handle errors when call object is not available', () => {
|
|
49
|
+
vapi['call'] = null; // Simulate call object not being available
|
|
50
|
+
expect(() => vapi.toggleMute()).toThrow('Call object is not available.');
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
package/jest.config.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Config } from '@jest/types';
|
|
2
|
+
|
|
3
|
+
const config: Config.InitialOptions = {
|
|
4
|
+
preset: 'ts-jest',
|
|
5
|
+
testEnvironment: 'node',
|
|
6
|
+
// Your Jest configuration options here
|
|
7
|
+
// For example, to match test files:
|
|
8
|
+
testMatch: ['**/__tests__/**/*.ts?(x)', '**/?(*.)+(spec|test).ts?(x)'],
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export default config;
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vapi-ai/web",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.258",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/vapi.js",
|
|
6
6
|
"types": "dist/vapi.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"test": "
|
|
8
|
+
"test": "jest",
|
|
9
9
|
"build": "tsc && tsc --declaration",
|
|
10
10
|
"start": "npm run generate-api && npm run build && node dist/vapi.js",
|
|
11
11
|
"generate-api": "npx swagger-typescript-api -p https://api.vapi.ai/api-json -o . -n api.ts",
|
|
@@ -26,8 +26,12 @@
|
|
|
26
26
|
"events": "^3.3.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
+
"@types/jest": "^29.5.12",
|
|
29
30
|
"@types/node": "^20.6.0",
|
|
31
|
+
"jest": "^29.7.0",
|
|
30
32
|
"openai": "^4.24.1",
|
|
31
|
-
"swagger-typescript-api": "^13.0.3"
|
|
33
|
+
"swagger-typescript-api": "^13.0.3",
|
|
34
|
+
"ts-jest": "^29.1.2",
|
|
35
|
+
"ts-node": "^10.9.2"
|
|
32
36
|
}
|
|
33
37
|
}
|
package/vapi.ts
CHANGED
|
@@ -178,9 +178,11 @@ export default class Vapi extends VapiEventEmitter {
|
|
|
178
178
|
});
|
|
179
179
|
|
|
180
180
|
this.call.startRemoteParticipantsAudioLevelObserver(100);
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
181
|
+
|
|
182
|
+
this.call.on("remote-participants-audio-level", (e) => {
|
|
183
|
+
if (e) this.handleRemoteParticipantsAudioLevel(e);
|
|
184
|
+
});
|
|
185
|
+
|
|
184
186
|
this.call.on("app-message", (e) => this.onAppMessage(e));
|
|
185
187
|
|
|
186
188
|
this.call.updateInputSettings({
|
|
@@ -205,9 +207,14 @@ export default class Vapi extends VapiEventEmitter {
|
|
|
205
207
|
try {
|
|
206
208
|
if (e.data === "listening") {
|
|
207
209
|
return this.emit("call-start");
|
|
210
|
+
} else {
|
|
211
|
+
try {
|
|
212
|
+
const parsedMessage = JSON.parse(e.data);
|
|
213
|
+
this.emit("message", parsedMessage);
|
|
214
|
+
} catch (parseError) {
|
|
215
|
+
console.log("Error parsing message data: ", parseError);
|
|
216
|
+
}
|
|
208
217
|
}
|
|
209
|
-
const parsedMessage = JSON.parse(e.data);
|
|
210
|
-
this.emit("message", parsedMessage);
|
|
211
218
|
} catch (e) {
|
|
212
219
|
console.error(e);
|
|
213
220
|
}
|
|
@@ -249,4 +256,29 @@ export default class Vapi extends VapiEventEmitter {
|
|
|
249
256
|
send(message: VapiClientToServerMessage): void {
|
|
250
257
|
this.call?.sendAppMessage(JSON.stringify(message));
|
|
251
258
|
}
|
|
259
|
+
|
|
260
|
+
private setMuted(mute: boolean) {
|
|
261
|
+
try {
|
|
262
|
+
if (!this.call) {
|
|
263
|
+
throw new Error('Call object is not available.');
|
|
264
|
+
}
|
|
265
|
+
this.call.setLocalAudio(!mute);
|
|
266
|
+
} catch (error) {
|
|
267
|
+
console.error("Error in setMuted:", error);
|
|
268
|
+
throw error;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
public toggleMute() {
|
|
273
|
+
try {
|
|
274
|
+
if (!this.call) {
|
|
275
|
+
throw new Error('Call object is not available.');
|
|
276
|
+
}
|
|
277
|
+
const isCurrentlyMuted = this.call.localAudio() === false;
|
|
278
|
+
this.setMuted(!isCurrentlyMuted);
|
|
279
|
+
} catch (error) {
|
|
280
|
+
console.error("Error in toggleMute:", error);
|
|
281
|
+
throw error;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
252
284
|
}
|