@trillet-ai/web-sdk 1.1.1 → 1.1.3

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.d.ts CHANGED
@@ -7,6 +7,7 @@ export interface TrilletAgentConfig {
7
7
  }
8
8
  export interface TrilletCallConfig {
9
9
  token: string;
10
+ wsUrl: string;
10
11
  audioSettings?: {
11
12
  sampleRate?: number;
12
13
  inputDeviceId?: string;
package/dist/index.js CHANGED
@@ -1,11 +1,27 @@
1
1
  import { EventEmitter } from "eventemitter3";
2
2
  import { RemoteAudioTrack, Room, RoomEvent, Track, createAudioAnalyser, } from "livekit-client";
3
3
  const TRILLET_API_URL = "https://api.trillet.ai/v1/api/web-call";
4
- const TRILLET_WS_URL = "wss://trilletai-ek0j3un1.livekit.cloud";
5
4
  const decoder = new TextDecoder();
5
+ // Browser compatibility check
6
+ const isBrowser = typeof window !== 'undefined';
7
+ const isSecureContext = isBrowser && window.isSecureContext;
6
8
  export class TrilletAgent extends EventEmitter {
7
9
  constructor(config) {
8
10
  super();
11
+ // Validate environment
12
+ if (!isBrowser) {
13
+ throw new Error('Trillet SDK must be used in a browser environment');
14
+ }
15
+ if (!isSecureContext) {
16
+ throw new Error('Trillet SDK requires a secure context (HTTPS or localhost)');
17
+ }
18
+ // Validate config
19
+ if (!config.apiKey)
20
+ throw new Error('apiKey is required');
21
+ if (!config.workspaceId)
22
+ throw new Error('workspaceId is required');
23
+ if (!config.agentId)
24
+ throw new Error('agentId is required');
9
25
  this.config = config;
10
26
  this.sdk = new TrilletWebSDK();
11
27
  this.setupEventForwarding();
@@ -22,6 +38,7 @@ export class TrilletAgent extends EventEmitter {
22
38
  this.sdk.on('audioData', (data) => this.emit('audioData', data));
23
39
  }
24
40
  async startCall() {
41
+ var _a;
25
42
  try {
26
43
  // 1. Get LiveKit token from Trillet API
27
44
  const response = await fetch(TRILLET_API_URL, {
@@ -30,24 +47,32 @@ export class TrilletAgent extends EventEmitter {
30
47
  'Content-Type': 'application/json',
31
48
  'Accept': 'application/json',
32
49
  'x-workspace-id': this.config.workspaceId,
33
- 'x-api-key': this.config.apiKey,
34
- 'Origin': window.location.origin
50
+ 'x-api-key': this.config.apiKey
35
51
  },
36
- mode: 'cors',
37
- credentials: 'omit',
38
52
  body: JSON.stringify({
39
53
  call_agent_id: this.config.agentId,
40
54
  dynamic_variables: this.config.variables || {}
41
55
  })
42
56
  });
43
57
  if (!response.ok) {
44
- const error = await response.text();
45
- throw new Error(`API Error: ${error}`);
58
+ let errorMessage;
59
+ try {
60
+ const errorData = await response.json();
61
+ errorMessage = errorData.message || errorData.error || 'Unknown error';
62
+ }
63
+ catch (_b) {
64
+ errorMessage = await response.text();
65
+ }
66
+ throw new Error(`API Error: ${errorMessage}`);
46
67
  }
47
68
  const data = await response.json();
69
+ if (!data.token || !((_a = data.connection) === null || _a === void 0 ? void 0 : _a.url)) {
70
+ throw new Error('Invalid response from server: missing token or connection details');
71
+ }
48
72
  // 2. Initialize call with LiveKit token
49
73
  await this.sdk.initializeCall({
50
74
  token: data.token,
75
+ wsUrl: data.connection.url,
51
76
  audioSettings: {
52
77
  sampleRate: 48000
53
78
  },
@@ -59,7 +84,8 @@ export class TrilletAgent extends EventEmitter {
59
84
  await this.sdk.enableAudioPlayback();
60
85
  }
61
86
  catch (error) {
62
- this.emit('error', error);
87
+ console.error('Trillet SDK Error:', error);
88
+ this.emit('error', error instanceof Error ? error : new Error(String(error)));
63
89
  throw error;
64
90
  }
65
91
  }
@@ -98,16 +124,18 @@ class TrilletWebSDK extends EventEmitter {
98
124
  },
99
125
  });
100
126
  this.setupEventHandlers(config);
101
- await this.room.connect(TRILLET_WS_URL, config.token);
127
+ await this.room.connect(config.wsUrl, config.token);
102
128
  console.log("Connected to Trillet room:", this.room.name);
103
129
  this.room.localParticipant.setMicrophoneEnabled(true);
104
130
  this.isConnected = true;
105
131
  this.emit("connected");
106
132
  }
107
133
  catch (error) {
108
- this.emit("error", "Failed to initialize call");
134
+ const errorMessage = error instanceof Error ? error.message : 'Failed to initialize call';
135
+ this.emit("error", errorMessage);
109
136
  console.error("Call initialization failed:", error);
110
137
  this.endCall();
138
+ throw error; // Re-throw to let TrilletAgent handle it
111
139
  }
112
140
  }
113
141
  async enableAudioPlayback() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trillet-ai/web-sdk",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Trillet Web SDK for real-time audio communication with AI agents",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",