Skip to content

Commit

Permalink
Merge pull request #79 from VapiAI/bryant/add-join-delay-for-mobile-d…
Browse files Browse the repository at this point in the history
…evices

2.1.8: add join delay for mobile devices
  • Loading branch information
bryantanderson authored Nov 25, 2024
2 parents ecfb47b + 8bc340c commit c7e188c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 86 deletions.
43 changes: 2 additions & 41 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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": {
Expand Down
106 changes: 63 additions & 43 deletions vapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<E extends VapiEventNames>(
event: E,
Expand Down Expand Up @@ -171,6 +171,20 @@ 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,
Expand Down Expand Up @@ -263,6 +277,12 @@ 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);
}

await this.call.join({
url: webCall.webCallUrl,
subscribeToTracksAutomatically: false,
Expand Down

0 comments on commit c7e188c

Please sign in to comment.