From 337134d61406f3773af267e43ff04f183c1ddc50 Mon Sep 17 00:00:00 2001 From: Bryant Date: Thu, 7 Nov 2024 14:59:52 +1100 Subject: [PATCH 1/6] chore: add small delay before joining daily room for mobile devices --- vapi.ts | 106 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 46 deletions(-) diff --git a/vapi.ts b/vapi.ts index a26ebfb..7f45106 100644 --- a/vapi.ts +++ b/vapi.ts @@ -18,6 +18,49 @@ import { } from './api'; import { client } from './client'; +export interface AddMessageMessage { + type: 'add-message'; + message: ChatCompletionMessageParam; +} + +export interface ControlMessages { + type: 'control'; + control: 'mute-assistant' | 'unmute-assistant' | 'say-first-message'; + videoRecordingStartDelaySeconds?: number; +} + +export interface SayMessage { + type: 'say'; + message: string; + endCallAfterSpoken?: boolean; +} + +type VapiClientToServerMessage = + | AddMessageMessage + | ControlMessages + | SayMessage; + +type VapiEventNames = + | 'call-end' + | 'call-start' + | 'volume-level' + | 'speech-start' + | 'speech-end' + | 'message' + | 'video' + | 'error'; + +type VapiEventListeners = { + 'call-end': () => void; + 'call-start': () => void; + 'volume-level': (volume: number) => void; + 'speech-start': () => void; + 'speech-end': () => void; + video: (track: MediaStreamTrack) => void; + message: (message: any) => void; + error: (error: any) => void; +}; + async function startAudioPlayer( player: HTMLAudioElement, track: MediaStreamTrack, @@ -64,49 +107,6 @@ function subscribeToTracks( }); } -export interface AddMessageMessage { - type: 'add-message'; - message: ChatCompletionMessageParam; -} - -export interface ControlMessages { - type: 'control'; - control: 'mute-assistant' | 'unmute-assistant' | 'say-first-message'; - videoRecordingStartDelaySeconds?: number; -} - -export interface SayMessage { - type: 'say'; - message: string; - endCallAfterSpoken?: boolean; -} - -type VapiClientToServerMessage = - | AddMessageMessage - | ControlMessages - | SayMessage; - -type VapiEventNames = - | 'call-end' - | 'call-start' - | 'volume-level' - | 'speech-start' - | 'speech-end' - | 'message' - | 'video' - | 'error'; - -type VapiEventListeners = { - 'call-end': () => void; - 'call-start': () => void; - 'volume-level': (volume: number) => void; - 'speech-start': () => void; - 'speech-end': () => void; - video: (track: MediaStreamTrack) => void; - message: (message: any) => void; - error: (error: any) => void; -}; - class VapiEventEmitter extends EventEmitter { on( event: E, @@ -171,6 +171,18 @@ export default class Vapi extends VapiEventEmitter { this.speakingTimeout = null; } + private isMobileDevice() { + if (typeof navigator === 'undefined') return false; + const userAgent = navigator.userAgent; + return /android|iphone|ipad|ipod|iemobile|blackberry|bada/i.test( + userAgent.toLowerCase(), + ); + } + + private async sleep(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)); + } + async start( assistant?: CreateAssistantDTO | string, assistantOverrides?: AssistantOverrides, @@ -196,9 +208,7 @@ export default class Vapi extends VapiEventEmitter { }) ).data; - if (this.call) { - this.cleanup(); - } + if (this.call) this.cleanup(); const isVideoRecordingEnabled = webCall?.artifactPlan?.videoRecordingEnabled ?? false; @@ -263,6 +273,10 @@ export default class Vapi extends VapiEventEmitter { ); }); + // Allow mobile devices to finish processing the microphone permissions + // request before joining the call and playing the assistant's audio + if (this.isMobileDevice()) await this.sleep(250); + await this.call.join({ url: webCall.webCallUrl, subscribeToTracksAutomatically: false, From c715e91fa2d69b76ad8280070a512c02e8385219 Mon Sep 17 00:00:00 2001 From: Bryant Date: Thu, 7 Nov 2024 15:02:32 +1100 Subject: [PATCH 2/6] chore: bump version to 2.1.8 --- package-lock.json | 43 ++----------------------------------------- package.json | 3 +-- 2 files changed, 3 insertions(+), 43 deletions(-) diff --git a/package-lock.json b/package-lock.json index 06799e4..ef634ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,15 @@ { "name": "@vapi-ai/web", - "version": "2.1.7", + "version": "2.1.8", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@vapi-ai/web", - "version": "2.1.7", + "version": "2.1.8", "license": "MIT", "dependencies": { "@daily-co/daily-js": "^0.72.1", - "@vapi-ai/web": "^2.1.5", "events": "^3.3.0" }, "devDependencies": { @@ -1625,44 +1624,6 @@ "integrity": "sha512-I4q9QU9MQv4oEOz4tAHJtNz1cwuLxn2F3xcc2iV5WdqLPpUnj30aUuxt1mAxYTG+oe8CZMV/+6rU4S4gRDzqtQ==", "dev": true }, - "node_modules/@vapi-ai/web": { - "version": "2.1.5", - "resolved": "https://registry.npmjs.org/@vapi-ai/web/-/web-2.1.5.tgz", - "integrity": "sha512-ah77kAmvPFFHtCkN3I9YL7JLZDapwat+bCIBCAg5nh2iJR2TnKF3CqQJ+yRQjuTHpGV1PFG5ywNJwxchu5Rkjw==", - "license": "MIT", - "dependencies": { - "@daily-co/daily-js": "^0.66.0", - "@vapi-ai/web": "^1.0.263", - "events": "^3.3.0" - } - }, - "node_modules/@vapi-ai/web/node_modules/@daily-co/daily-js": { - "version": "0.66.0", - "resolved": "https://registry.npmjs.org/@daily-co/daily-js/-/daily-js-0.66.0.tgz", - "integrity": "sha512-TkfOEs99nLOqWCuF3XgdZ+xrsP+lUXjINAfENjsly98x3o7gmMzQ+bWVx6pkozUkDqu3yhXI0oIHHB0CwX/RsA==", - "license": "BSD-2-Clause", - "dependencies": { - "@babel/runtime": "^7.12.5", - "@sentry/browser": "^7.60.1", - "bowser": "^2.8.1", - "dequal": "^2.0.3", - "events": "^3.1.0" - }, - "engines": { - "node": ">=10.0.0" - } - }, - "node_modules/@vapi-ai/web/node_modules/@vapi-ai/web": { - "version": "1.4.2", - "resolved": "https://registry.npmjs.org/@vapi-ai/web/-/web-1.4.2.tgz", - "integrity": "sha512-HRwN5j0br7FMBOyIfZHC4JOj0XDc/jXy2VWvinU5OZTeUJocnmQcVLgUjWwYEWt8XQSw94gIJ2U/qPOm5def8w==", - "license": "MIT", - "dependencies": { - "@daily-co/daily-js": "^0.66.0", - "@vapi-ai/web": "^1.0.263", - "events": "^3.3.0" - } - }, "node_modules/abort-controller": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", diff --git a/package.json b/package.json index 80e58cc..4043200 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@vapi-ai/web", - "version": "2.1.7", + "version": "2.1.8", "description": "", "main": "dist/vapi.js", "types": "dist/vapi.d.ts", @@ -23,7 +23,6 @@ "homepage": "https://github.com/VapiAI/web#readme", "dependencies": { "@daily-co/daily-js": "^0.72.1", - "@vapi-ai/web": "^2.1.5", "events": "^3.3.0" }, "devDependencies": { From 680a9bbbda863d93b0a4fc9975aabc6243b02f56 Mon Sep 17 00:00:00 2001 From: Bryant Date: Thu, 7 Nov 2024 15:26:25 +1100 Subject: [PATCH 3/6] chore: raise delay to 500ms --- vapi.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vapi.ts b/vapi.ts index 7f45106..fc5dc2f 100644 --- a/vapi.ts +++ b/vapi.ts @@ -275,7 +275,7 @@ export default class Vapi extends VapiEventEmitter { // Allow mobile devices to finish processing the microphone permissions // request before joining the call and playing the assistant's audio - if (this.isMobileDevice()) await this.sleep(250); + if (this.isMobileDevice()) await this.sleep(500); await this.call.join({ url: webCall.webCallUrl, From 2db6ad1f37c2ed96124f237f5e4e2328fb693ee0 Mon Sep 17 00:00:00 2001 From: Bryant Date: Sat, 23 Nov 2024 15:15:28 +1100 Subject: [PATCH 4/6] bump mobile timeout --- vapi.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vapi.ts b/vapi.ts index fc5dc2f..6751c72 100644 --- a/vapi.ts +++ b/vapi.ts @@ -275,7 +275,7 @@ export default class Vapi extends VapiEventEmitter { // Allow mobile devices to finish processing the microphone permissions // request before joining the call and playing the assistant's audio - if (this.isMobileDevice()) await this.sleep(500); + if (this.isMobileDevice()) await this.sleep(1000); await this.call.join({ url: webCall.webCallUrl, From 1bd4465c50c3af9e85987d5f14224e2f06916200 Mon Sep 17 00:00:00 2001 From: Bryant Date: Tue, 26 Nov 2024 02:28:41 +1100 Subject: [PATCH 5/6] clean up --- vapi.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/vapi.ts b/vapi.ts index 6751c72..92aa9cf 100644 --- a/vapi.ts +++ b/vapi.ts @@ -208,7 +208,9 @@ export default class Vapi extends VapiEventEmitter { }) ).data; - if (this.call) this.cleanup(); + if (this.call) { + this.cleanup(); + } const isVideoRecordingEnabled = webCall?.artifactPlan?.videoRecordingEnabled ?? false; @@ -275,7 +277,9 @@ export default class Vapi extends VapiEventEmitter { // Allow mobile devices to finish processing the microphone permissions // request before joining the call and playing the assistant's audio - if (this.isMobileDevice()) await this.sleep(1000); + if (this.isMobileDevice()) { + await this.sleep(1000); + } await this.call.join({ url: webCall.webCallUrl, From 8bc340c8fd972d91e351e99d9be158126f7803fd Mon Sep 17 00:00:00 2001 From: Bryant Date: Tue, 26 Nov 2024 02:29:26 +1100 Subject: [PATCH 6/6] clean up --- vapi.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vapi.ts b/vapi.ts index 92aa9cf..08ab590 100644 --- a/vapi.ts +++ b/vapi.ts @@ -172,7 +172,9 @@ export default class Vapi extends VapiEventEmitter { } private isMobileDevice() { - if (typeof navigator === 'undefined') return false; + if (typeof navigator === 'undefined') { + return false; + } const userAgent = navigator.userAgent; return /android|iphone|ipad|ipod|iemobile|blackberry|bada/i.test( userAgent.toLowerCase(),