Repository navigation
Expand file tree
/
Copy pathvapiCallStorage.ts
More file actions
141 lines (121 loc) · 3.43 KB
/
Copy pathvapiCallStorage.ts
File metadata and controls
141 lines (121 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import Cookies from 'js-cookie';
export interface StoredCallData {
webCallUrl: string;
id?: string;
artifactPlan?: {
videoRecordingEnabled?: boolean;
};
assistant?: {
voice?: {
provider?: string;
};
};
callOptions?: any;
timestamp: number;
}
export type StorageType = 'session' | 'cookies';
// Get root domain for cookie (e.g., ".domain.com")
function getRootDomain(): string {
const hostname = window.location.hostname;
// Handle localhost/IP
if (
hostname === 'localhost' ||
hostname === '127.0.0.1' ||
/^\d+\.\d+\.\d+\.\d+$/.test(hostname)
) {
return hostname;
}
const parts = hostname.split('.');
// Already root domain
if (parts.length <= 2) {
return hostname;
}
// Return root domain with leading dot (e.g., ".domain.com")
return '.' + parts.slice(-2).join('.');
}
export const storeCallData = (
reconnectStorageKey: string,
call: any,
callOptions?: any,
storageType: StorageType = 'session'
) => {
const webCallUrl =
(call as any).webCallUrl || (call.transport as any)?.callUrl;
if (!webCallUrl) {
console.warn(
'No webCallUrl found in call object, cannot store for reconnection'
);
return;
}
const webCallToStore: StoredCallData = {
webCallUrl,
id: call.id,
artifactPlan: call.artifactPlan,
assistant: call.assistant,
callOptions,
timestamp: Date.now(),
};
if (storageType === 'session') {
sessionStorage.setItem(reconnectStorageKey, JSON.stringify(webCallToStore));
} else if (storageType === 'cookies') {
try {
const rootDomain = getRootDomain();
Cookies.set(reconnectStorageKey, JSON.stringify(webCallToStore), {
domain: rootDomain,
path: '/',
secure: true,
sameSite: 'lax',
expires: 1 / 24, // 1 hour (expires takes days, so 1/24 = 1 hour)
});
} catch (error) {
console.error('Failed to store call data in cookie:', error);
}
}
};
export const getStoredCallData = (
reconnectStorageKey: string,
storageType: StorageType = 'session'
): StoredCallData | null => {
try {
if (storageType === 'session') {
const sessionData = sessionStorage.getItem(reconnectStorageKey);
if (!sessionData) return null;
return JSON.parse(sessionData);
} else if (storageType === 'cookies') {
const cookieValue = Cookies.get(reconnectStorageKey);
if (!cookieValue) return null;
return JSON.parse(cookieValue);
}
return null;
} catch (error) {
console.error('Error reading stored call data:', error);
return null;
}
};
export const clearStoredCall = (
reconnectStorageKey: string,
storageType: StorageType = 'session'
) => {
if (storageType === 'session') {
sessionStorage.removeItem(reconnectStorageKey);
} else if (storageType === 'cookies') {
const rootDomain = getRootDomain();
Cookies.remove(reconnectStorageKey, {
domain: rootDomain,
path: '/',
});
}
};
export const areCallOptionsEqual = (options1: any, options2: any): boolean => {
// Handle null/undefined cases
if (options1 === options2) return true;
if (!options1 || !options2) return false;
try {
// Deep comparison using JSON serialization
// This works for most cases but may not handle functions, dates, etc.
return JSON.stringify(options1) === JSON.stringify(options2);
} catch {
// Fallback to reference equality if JSON.stringify fails
return options1 === options2;
}
};