@trillet-ai/web-sdk 1.7.0 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +86 -57
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -11,7 +11,13 @@ const TRILLET_API_URL = {
|
|
|
11
11
|
const decoder = new TextDecoder();
|
|
12
12
|
// Browser compatibility check
|
|
13
13
|
const isBrowser = typeof window !== 'undefined';
|
|
14
|
-
|
|
14
|
+
// Safari iOS in private mode (and some proxy configs) returns isSecureContext=false
|
|
15
|
+
// even on HTTPS. Fall back to protocol/hostname so we don't block valid contexts.
|
|
16
|
+
const isSecureContext = isBrowser &&
|
|
17
|
+
(window.isSecureContext ||
|
|
18
|
+
window.location.protocol === 'https:' ||
|
|
19
|
+
window.location.hostname === 'localhost' ||
|
|
20
|
+
window.location.hostname === '127.0.0.1');
|
|
15
21
|
export class TrilletAgent extends EventEmitter {
|
|
16
22
|
constructor(config) {
|
|
17
23
|
super();
|
|
@@ -96,8 +102,15 @@ export class TrilletAgent extends EventEmitter {
|
|
|
96
102
|
}
|
|
97
103
|
async startCall() {
|
|
98
104
|
try {
|
|
99
|
-
// Generate browser fingerprint before making the call
|
|
100
|
-
|
|
105
|
+
// Generate browser fingerprint before making the call.
|
|
106
|
+
// Non-fatal: see startPublicCall for rationale.
|
|
107
|
+
let browserFingerprint = null;
|
|
108
|
+
try {
|
|
109
|
+
browserFingerprint = await BrowserFingerprint.generate();
|
|
110
|
+
}
|
|
111
|
+
catch (fpError) {
|
|
112
|
+
console.warn('Trillet SDK: fingerprint generation failed, proceeding without it', fpError);
|
|
113
|
+
}
|
|
101
114
|
// 2. Initialize call with API KEY
|
|
102
115
|
const response = await fetch(TRILLET_API_URL.CONNECT, {
|
|
103
116
|
method: 'POST',
|
|
@@ -132,7 +145,7 @@ export class TrilletAgent extends EventEmitter {
|
|
|
132
145
|
// Initialize call with LiveKit token
|
|
133
146
|
await this.sdk.initializeCall({
|
|
134
147
|
token: data.token,
|
|
135
|
-
wsUrl: 'wss://trillet-ai-xdx0dw5r.livekit.cloud',
|
|
148
|
+
wsUrl: process.env.LIVEKIT_WS_URL || 'wss://trillet-ai-xdx0dw5r.livekit.cloud',
|
|
136
149
|
mode: this.config.mode,
|
|
137
150
|
audioSettings: {
|
|
138
151
|
sampleRate: 48000,
|
|
@@ -161,64 +174,80 @@ export class TrilletAgent extends EventEmitter {
|
|
|
161
174
|
}
|
|
162
175
|
}
|
|
163
176
|
async startPublicCall() {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
'Content-Type': 'application/json',
|
|
171
|
-
Accept: 'application/json',
|
|
172
|
-
'x-workspace-id': this.config.workspaceId,
|
|
173
|
-
},
|
|
174
|
-
body: JSON.stringify({
|
|
175
|
-
call_agent_id: this.config.agentId,
|
|
176
|
-
mode: this.config.mode,
|
|
177
|
-
dynamic_variables: this.config.variables || {},
|
|
178
|
-
callback_url: this.config.callbackUrl,
|
|
179
|
-
browser_fingerprint: browserFingerprint,
|
|
180
|
-
demo_hash: this.config.demoHash, // Send demo hash if provided
|
|
181
|
-
}),
|
|
182
|
-
});
|
|
183
|
-
if (!response.ok) {
|
|
184
|
-
let errorMessage;
|
|
177
|
+
try {
|
|
178
|
+
// Generate browser fingerprint before making the call.
|
|
179
|
+
// Non-fatal: if it fails (Safari iOS quirks with crypto.subtle / canvas.toDataURL
|
|
180
|
+
// in private mode or restricted contexts), fall back to null so the fetch still
|
|
181
|
+
// happens. Backend's per-user limit tracking degrades gracefully.
|
|
182
|
+
let browserFingerprint = null;
|
|
185
183
|
try {
|
|
186
|
-
|
|
187
|
-
errorMessage = errorData.message || errorData.error || 'Unknown error';
|
|
184
|
+
browserFingerprint = await BrowserFingerprint.generate();
|
|
188
185
|
}
|
|
189
|
-
catch (
|
|
190
|
-
|
|
186
|
+
catch (fpError) {
|
|
187
|
+
console.warn('Trillet SDK: fingerprint generation failed, proceeding without it', fpError);
|
|
191
188
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
189
|
+
// 2. Initialize public call
|
|
190
|
+
const response = await fetch(TRILLET_API_URL.PUBLIC_CONNECT, {
|
|
191
|
+
method: 'POST',
|
|
192
|
+
headers: {
|
|
193
|
+
'Content-Type': 'application/json',
|
|
194
|
+
Accept: 'application/json',
|
|
195
|
+
'x-workspace-id': this.config.workspaceId,
|
|
196
|
+
},
|
|
197
|
+
body: JSON.stringify({
|
|
198
|
+
call_agent_id: this.config.agentId,
|
|
199
|
+
mode: this.config.mode,
|
|
200
|
+
dynamic_variables: this.config.variables || {},
|
|
201
|
+
callback_url: this.config.callbackUrl,
|
|
202
|
+
browser_fingerprint: browserFingerprint,
|
|
203
|
+
demo_hash: this.config.demoHash, // Send demo hash if provided
|
|
204
|
+
}),
|
|
205
|
+
});
|
|
206
|
+
if (!response.ok) {
|
|
207
|
+
let errorMessage;
|
|
208
|
+
try {
|
|
209
|
+
const errorData = await response.json();
|
|
210
|
+
errorMessage = errorData.message || errorData.error || 'Unknown error';
|
|
211
|
+
}
|
|
212
|
+
catch (_a) {
|
|
213
|
+
errorMessage = await response.text();
|
|
214
|
+
}
|
|
215
|
+
throw new Error(`API Error: ${errorMessage}`);
|
|
216
|
+
}
|
|
217
|
+
const data = await response.json();
|
|
218
|
+
if (!data.token || !data.roomName) {
|
|
219
|
+
throw new Error('Invalid response from server: missing token or room name');
|
|
220
|
+
}
|
|
221
|
+
// Initialize call with LiveKit token
|
|
222
|
+
await this.sdk.initializeCall({
|
|
223
|
+
token: data.token,
|
|
224
|
+
wsUrl: 'wss://trillet-ai-xdx0dw5r.livekit.cloud',
|
|
225
|
+
mode: this.config.mode,
|
|
226
|
+
audioSettings: {
|
|
227
|
+
sampleRate: 48000,
|
|
228
|
+
},
|
|
229
|
+
features: {
|
|
230
|
+
enableRawAudio: this.config.mode === 'voice',
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
// Enable audio playback for voice mode
|
|
234
|
+
if (this.config.mode === 'voice') {
|
|
235
|
+
await this.sdk.enableAudioPlayback();
|
|
236
|
+
}
|
|
237
|
+
// Set up default RPC handlers for text communication
|
|
238
|
+
this.setupDefaultRpcHandlers();
|
|
239
|
+
// Emit additional connection details
|
|
240
|
+
this.emit('connected', {
|
|
241
|
+
callId: data.callId,
|
|
242
|
+
roomName: data.roomName,
|
|
243
|
+
agent: data.agent,
|
|
244
|
+
});
|
|
197
245
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
mode: this.config.mode,
|
|
203
|
-
audioSettings: {
|
|
204
|
-
sampleRate: 48000,
|
|
205
|
-
},
|
|
206
|
-
features: {
|
|
207
|
-
enableRawAudio: this.config.mode === 'voice',
|
|
208
|
-
},
|
|
209
|
-
});
|
|
210
|
-
// Enable audio playback for voice mode
|
|
211
|
-
if (this.config.mode === 'voice') {
|
|
212
|
-
await this.sdk.enableAudioPlayback();
|
|
246
|
+
catch (error) {
|
|
247
|
+
console.error('Trillet SDK Error (public call):', error);
|
|
248
|
+
this.emit('error', error instanceof Error ? error : new Error(String(error)));
|
|
249
|
+
throw error;
|
|
213
250
|
}
|
|
214
|
-
// Set up default RPC handlers for text communication
|
|
215
|
-
this.setupDefaultRpcHandlers();
|
|
216
|
-
// Emit additional connection details
|
|
217
|
-
this.emit('connected', {
|
|
218
|
-
callId: data.callId,
|
|
219
|
-
roomName: data.roomName,
|
|
220
|
-
agent: data.agent,
|
|
221
|
-
});
|
|
222
251
|
}
|
|
223
252
|
endCall() {
|
|
224
253
|
this.sdk.endCall();
|