@trillet-ai/web-sdk 1.1.1 → 1.1.2
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 +33 -5
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3,9 +3,26 @@ import { RemoteAudioTrack, Room, RoomEvent, Track, createAudioAnalyser, } from "
|
|
|
3
3
|
const TRILLET_API_URL = "https://api.trillet.ai/v1/api/web-call";
|
|
4
4
|
const TRILLET_WS_URL = "wss://trilletai-ek0j3un1.livekit.cloud";
|
|
5
5
|
const decoder = new TextDecoder();
|
|
6
|
+
// Browser compatibility check
|
|
7
|
+
const isBrowser = typeof window !== 'undefined';
|
|
8
|
+
const isSecureContext = isBrowser && window.isSecureContext;
|
|
6
9
|
export class TrilletAgent extends EventEmitter {
|
|
7
10
|
constructor(config) {
|
|
8
11
|
super();
|
|
12
|
+
// Validate environment
|
|
13
|
+
if (!isBrowser) {
|
|
14
|
+
throw new Error('Trillet SDK must be used in a browser environment');
|
|
15
|
+
}
|
|
16
|
+
if (!isSecureContext) {
|
|
17
|
+
throw new Error('Trillet SDK requires a secure context (HTTPS or localhost)');
|
|
18
|
+
}
|
|
19
|
+
// Validate config
|
|
20
|
+
if (!config.apiKey)
|
|
21
|
+
throw new Error('apiKey is required');
|
|
22
|
+
if (!config.workspaceId)
|
|
23
|
+
throw new Error('workspaceId is required');
|
|
24
|
+
if (!config.agentId)
|
|
25
|
+
throw new Error('agentId is required');
|
|
9
26
|
this.config = config;
|
|
10
27
|
this.sdk = new TrilletWebSDK();
|
|
11
28
|
this.setupEventForwarding();
|
|
@@ -22,6 +39,7 @@ export class TrilletAgent extends EventEmitter {
|
|
|
22
39
|
this.sdk.on('audioData', (data) => this.emit('audioData', data));
|
|
23
40
|
}
|
|
24
41
|
async startCall() {
|
|
42
|
+
var _a;
|
|
25
43
|
try {
|
|
26
44
|
// 1. Get LiveKit token from Trillet API
|
|
27
45
|
const response = await fetch(TRILLET_API_URL, {
|
|
@@ -30,21 +48,30 @@ export class TrilletAgent extends EventEmitter {
|
|
|
30
48
|
'Content-Type': 'application/json',
|
|
31
49
|
'Accept': 'application/json',
|
|
32
50
|
'x-workspace-id': this.config.workspaceId,
|
|
33
|
-
'x-api-key': this.config.apiKey
|
|
34
|
-
'Origin': window.location.origin
|
|
51
|
+
'x-api-key': this.config.apiKey
|
|
35
52
|
},
|
|
36
53
|
mode: 'cors',
|
|
37
|
-
credentials: '
|
|
54
|
+
credentials: 'same-origin',
|
|
38
55
|
body: JSON.stringify({
|
|
39
56
|
call_agent_id: this.config.agentId,
|
|
40
57
|
dynamic_variables: this.config.variables || {}
|
|
41
58
|
})
|
|
42
59
|
});
|
|
43
60
|
if (!response.ok) {
|
|
44
|
-
|
|
45
|
-
|
|
61
|
+
let errorMessage;
|
|
62
|
+
try {
|
|
63
|
+
const errorData = await response.json();
|
|
64
|
+
errorMessage = errorData.message || errorData.error || 'Unknown error';
|
|
65
|
+
}
|
|
66
|
+
catch (_b) {
|
|
67
|
+
errorMessage = await response.text();
|
|
68
|
+
}
|
|
69
|
+
throw new Error(`API Error: ${errorMessage}`);
|
|
46
70
|
}
|
|
47
71
|
const data = await response.json();
|
|
72
|
+
if (!data.token || !((_a = data.connection) === null || _a === void 0 ? void 0 : _a.url)) {
|
|
73
|
+
throw new Error('Invalid response from server: missing token or connection details');
|
|
74
|
+
}
|
|
48
75
|
// 2. Initialize call with LiveKit token
|
|
49
76
|
await this.sdk.initializeCall({
|
|
50
77
|
token: data.token,
|
|
@@ -59,6 +86,7 @@ export class TrilletAgent extends EventEmitter {
|
|
|
59
86
|
await this.sdk.enableAudioPlayback();
|
|
60
87
|
}
|
|
61
88
|
catch (error) {
|
|
89
|
+
console.error('Trillet SDK Error:', error);
|
|
62
90
|
this.emit('error', error);
|
|
63
91
|
throw error;
|
|
64
92
|
}
|