Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions daily-guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import type {
DailyCall,
DailyAdvancedConfig,
DailyFactoryOptions,
} from '@daily-co/daily-js';

export interface SafeDailyAdvancedConfig extends Omit<DailyAdvancedConfig, 'alwaysIncludeMicInPermissionPrompt'> {
alwaysIncludeMicInPermissionPrompt?: true; // Only allow true
}

export interface SafeDailyFactoryOptions extends Omit<DailyFactoryOptions, 'audioSource'> {
audioSource?: string | boolean | MediaStreamTrack;
}

export function createSafeDailyConfig(
config?: Pick<DailyAdvancedConfig, 'avoidEval' | 'alwaysIncludeMicInPermissionPrompt'>
): SafeDailyAdvancedConfig {
if (!config) return {};

const { alwaysIncludeMicInPermissionPrompt, ...rest } = config;

// Force true or remove the property entirely. This can cause Chrome 140+ issues
if (alwaysIncludeMicInPermissionPrompt === false) {
console.warn(
'[Vapi] alwaysIncludeMicInPermissionPrompt:false detected. ' +
'This can cause Chrome 140+ issues. Removing the property.'
);
return rest;
}

return config as SafeDailyAdvancedConfig;
}

export function safeSetLocalAudio(call: DailyCall | null, enabled: boolean): void {
if (!call) {
throw new Error('Call object is not available.');
}

// Never use forceDiscardTrack. This can cause Chrome 140+ issues
call.setLocalAudio(enabled);
}

export async function safeSetInputDevicesAsync(
call: DailyCall | null,
options: Parameters<DailyCall['setInputDevicesAsync']>[0]
): Promise<void> {
if (!call) {
throw new Error('Call object is not available.');
}

// Validate audioSource
if ('audioSource' in options && options.audioSource === false) {
console.warn(
'[Vapi] setInputDevicesAsync with audioSource:false detected. ' +
'This can cause Chrome 140+ issues. Using default device instead.'
);

const { audioSource, ...safeOptions } = options;
await call.setInputDevicesAsync(safeOptions);
return;
}

await call.setInputDevicesAsync(options);
}

export function createSafeDailyFactoryOptions(
options?: Pick<DailyFactoryOptions, 'audioSource' | 'startAudioOff'>
): SafeDailyFactoryOptions {
if (!options) return {};

// Ensure audioSource is never false
if (options.audioSource === false) {
console.warn(
'[Vapi] audioSource:false detected in factory options. ' +
'This can cause Chrome 140+ issues. Defaulting to true.'
);
return { ...options, audioSource: true };
}

return options;
}
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"node": ">=18.0.0"
},
"dependencies": {
"@daily-co/daily-js": "^0.80.0",
"@daily-co/daily-js": "^0.83.1",
"events": "^3.3.0"
},
"devDependencies": {
Expand Down
21 changes: 12 additions & 9 deletions vapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import {
WorkflowOverrides,
} from './api';
import { client } from './client';
import {
createSafeDailyConfig,
createSafeDailyFactoryOptions,
safeSetLocalAudio,
safeSetInputDevicesAsync,
} from './daily-guards';

export interface AddMessageMessage {
type: 'add-message';
Expand Down Expand Up @@ -206,8 +212,8 @@ export default class Vapi extends VapiEventEmitter {
super();
client.baseUrl = apiBaseUrl ?? 'https://api.vapi.ai';
client.setSecurityData(apiToken);
this.dailyCallConfig = dailyCallConfig ?? {};
this.dailyCallObject = dailyCallObject ?? {};
this.dailyCallConfig = createSafeDailyConfig(dailyCallConfig);
this.dailyCallObject = createSafeDailyFactoryOptions(dailyCallObject);
}

private cleanup() {
Expand Down Expand Up @@ -649,7 +655,7 @@ export default class Vapi extends VapiEventEmitter {
},
})
.then(() => {
this.call?.setLocalAudio(true);
safeSetLocalAudio(this.call, true);
});
}
});
Expand Down Expand Up @@ -805,10 +811,7 @@ export default class Vapi extends VapiEventEmitter {
}

public setMuted(mute: boolean) {
if (!this.call) {
throw new Error('Call object is not available.');
}
this.call.setLocalAudio(!mute);
safeSetLocalAudio(this.call, !mute);
}

public isMuted() {
Expand All @@ -832,7 +835,7 @@ export default class Vapi extends VapiEventEmitter {
public setInputDevicesAsync(
options: Parameters<DailyCall['setInputDevicesAsync']>[0],
) {
this.call?.setInputDevicesAsync(options);
return safeSetInputDevicesAsync(this.call, options);
}

public async increaseMicLevel(gain: number) {
Expand All @@ -855,7 +858,7 @@ export default class Vapi extends VapiEventEmitter {
gainNode.connect(destination);

const [boostedTrack] = destination.stream.getAudioTracks();
await this.call.setInputDevicesAsync({ audioSource: boostedTrack });
await safeSetInputDevicesAsync(this.call, { audioSource: boostedTrack });
} catch (error) {
console.error("Error adjusting microphone level:", error);
}
Expand Down