|
| 1 | +import type { |
| 2 | + DailyCall, |
| 3 | + DailyAdvancedConfig, |
| 4 | + DailyFactoryOptions, |
| 5 | +} from '@daily-co/daily-js'; |
| 6 | + |
| 7 | +export interface SafeDailyAdvancedConfig extends Omit<DailyAdvancedConfig, 'alwaysIncludeMicInPermissionPrompt'> { |
| 8 | + alwaysIncludeMicInPermissionPrompt?: true; // Only allow true |
| 9 | +} |
| 10 | + |
| 11 | +export interface SafeDailyFactoryOptions extends Omit<DailyFactoryOptions, 'audioSource'> { |
| 12 | + audioSource?: string | boolean | MediaStreamTrack; |
| 13 | +} |
| 14 | + |
| 15 | +export function createSafeDailyConfig( |
| 16 | + config?: Pick<DailyAdvancedConfig, 'avoidEval' | 'alwaysIncludeMicInPermissionPrompt'> |
| 17 | +): SafeDailyAdvancedConfig { |
| 18 | + if (!config) return {}; |
| 19 | + |
| 20 | + const { alwaysIncludeMicInPermissionPrompt, ...rest } = config; |
| 21 | + |
| 22 | + // Force true or remove the property entirely. This can cause Chrome 140+ issues |
| 23 | + if (alwaysIncludeMicInPermissionPrompt === false) { |
| 24 | + console.warn( |
| 25 | + '[Vapi] alwaysIncludeMicInPermissionPrompt:false detected. ' + |
| 26 | + 'This can cause Chrome 140+ issues. Removing the property.' |
| 27 | + ); |
| 28 | + return rest; |
| 29 | + } |
| 30 | + |
| 31 | + return config as SafeDailyAdvancedConfig; |
| 32 | +} |
| 33 | + |
| 34 | +export function safeSetLocalAudio(call: DailyCall | null, enabled: boolean): void { |
| 35 | + if (!call) { |
| 36 | + throw new Error('Call object is not available.'); |
| 37 | + } |
| 38 | + |
| 39 | + // Never use forceDiscardTrack. This can cause Chrome 140+ issues |
| 40 | + call.setLocalAudio(enabled); |
| 41 | +} |
| 42 | + |
| 43 | +export async function safeSetInputDevicesAsync( |
| 44 | + call: DailyCall | null, |
| 45 | + options: Parameters<DailyCall['setInputDevicesAsync']>[0] |
| 46 | +): Promise<void> { |
| 47 | + if (!call) { |
| 48 | + throw new Error('Call object is not available.'); |
| 49 | + } |
| 50 | + |
| 51 | + // Validate audioSource |
| 52 | + if ('audioSource' in options && options.audioSource === false) { |
| 53 | + console.warn( |
| 54 | + '[Vapi] setInputDevicesAsync with audioSource:false detected. ' + |
| 55 | + 'This can cause Chrome 140+ issues. Using default device instead.' |
| 56 | + ); |
| 57 | + |
| 58 | + const { audioSource, ...safeOptions } = options; |
| 59 | + await call.setInputDevicesAsync(safeOptions); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + await call.setInputDevicesAsync(options); |
| 64 | +} |
| 65 | + |
| 66 | +export function createSafeDailyFactoryOptions( |
| 67 | + options?: Pick<DailyFactoryOptions, 'audioSource' | 'startAudioOff'> |
| 68 | +): SafeDailyFactoryOptions { |
| 69 | + if (!options) return {}; |
| 70 | + |
| 71 | + // Ensure audioSource is never false |
| 72 | + if (options.audioSource === false) { |
| 73 | + console.warn( |
| 74 | + '[Vapi] audioSource:false detected in factory options. ' + |
| 75 | + 'This can cause Chrome 140+ issues. Defaulting to true.' |
| 76 | + ); |
| 77 | + return { ...options, audioSource: true }; |
| 78 | + } |
| 79 | + |
| 80 | + return options; |
| 81 | +} |
0 commit comments