@trillet-ai/web-sdk 1.2.16 → 1.3.0
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/README.md +7 -0
- package/dist/index.d.ts +22 -2
- package/dist/index.js +144 -71
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -107,6 +107,12 @@ agent.on('error', (error) => {
|
|
|
107
107
|
|
|
108
108
|
// End the call
|
|
109
109
|
agent.endCall();
|
|
110
|
+
|
|
111
|
+
// Get Finalized Transcripts:
|
|
112
|
+
agent.getTranscripts();
|
|
113
|
+
|
|
114
|
+
// Get Ongoing Transcripts:
|
|
115
|
+
agent.getCurrentTranscripts();
|
|
110
116
|
```
|
|
111
117
|
|
|
112
118
|
## API Endpoints
|
|
@@ -128,6 +134,7 @@ The SDK emits various events you can listen to:
|
|
|
128
134
|
- `assistantStartedSpeaking`: Fired when the AI agent starts speaking
|
|
129
135
|
- `assistantStoppedSpeaking`: Fired when the AI agent stops speaking
|
|
130
136
|
- `audioData`: Provides raw audio data (if enabled)
|
|
137
|
+
- `transcriptionReceived`: Provides transcript and participant
|
|
131
138
|
|
|
132
139
|
## Advanced Configuration
|
|
133
140
|
|
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export interface TrilletAgentConfig {
|
|
|
6
6
|
agentId: string;
|
|
7
7
|
variables?: Record<string, any>;
|
|
8
8
|
callbackUrl?: string;
|
|
9
|
-
mode?:
|
|
9
|
+
mode?: "voice" | "text";
|
|
10
10
|
}
|
|
11
11
|
export interface TrilletCallConfig {
|
|
12
12
|
token: string;
|
|
@@ -20,15 +20,35 @@ export interface TrilletCallConfig {
|
|
|
20
20
|
enableRawAudio?: boolean;
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
|
+
interface ToolUsed {
|
|
24
|
+
name: string;
|
|
25
|
+
args?: any;
|
|
26
|
+
status?: "pending" | "success" | "error";
|
|
27
|
+
}
|
|
28
|
+
interface Transcript {
|
|
29
|
+
role: "user" | "assistant";
|
|
30
|
+
text: string;
|
|
31
|
+
isFinal: boolean;
|
|
32
|
+
timestamp: Date;
|
|
33
|
+
participantId?: string;
|
|
34
|
+
toolUsed?: ToolUsed;
|
|
35
|
+
}
|
|
23
36
|
export declare class TrilletAgent extends EventEmitter {
|
|
24
37
|
private sdk;
|
|
25
38
|
private config;
|
|
26
39
|
private sdkToken;
|
|
40
|
+
private transcripts;
|
|
41
|
+
private currentTranscript;
|
|
27
42
|
constructor(config: TrilletAgentConfig);
|
|
28
43
|
private setupEventForwarding;
|
|
29
|
-
private authenticate;
|
|
30
44
|
startCall(): Promise<void>;
|
|
45
|
+
startPublicCall(): Promise<void>;
|
|
31
46
|
endCall(): void;
|
|
32
47
|
toggleMicrophone(enabled: boolean): void;
|
|
33
48
|
get isAssistantSpeaking(): boolean;
|
|
49
|
+
getTranscripts(): Transcript[];
|
|
50
|
+
getCurrentTranscript(): {
|
|
51
|
+
[key: string]: string;
|
|
52
|
+
};
|
|
34
53
|
}
|
|
54
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -2,108 +2,118 @@ import { EventEmitter } from "eventemitter3";
|
|
|
2
2
|
import { RemoteAudioTrack, Room, RoomEvent, Track, createAudioAnalyser, } from "livekit-client";
|
|
3
3
|
const TRILLET_API_URL = {
|
|
4
4
|
AUTH: "https://api.trillet.ai/v1/api/calls/sdk/auth",
|
|
5
|
-
CONNECT: "https://api.trillet.ai/v1/api/calls/web/connect"
|
|
5
|
+
CONNECT: "https://api.trillet.ai/v1/api/calls/web/connect",
|
|
6
|
+
PUBLIC_CONNECT: "https://api.trillet.ai/v1/api/web-call/public",
|
|
6
7
|
};
|
|
7
8
|
const decoder = new TextDecoder();
|
|
8
9
|
// Browser compatibility check
|
|
9
|
-
const isBrowser = typeof window !==
|
|
10
|
+
const isBrowser = typeof window !== "undefined";
|
|
10
11
|
const isSecureContext = isBrowser && window.isSecureContext;
|
|
11
12
|
export class TrilletAgent extends EventEmitter {
|
|
12
13
|
constructor(config) {
|
|
13
14
|
super();
|
|
14
15
|
this.sdkToken = null;
|
|
16
|
+
this.transcripts = [];
|
|
17
|
+
this.currentTranscript = {};
|
|
15
18
|
// Validate environment
|
|
16
19
|
if (!isBrowser) {
|
|
17
|
-
throw new Error(
|
|
20
|
+
throw new Error("Trillet SDK must be used in a browser environment");
|
|
18
21
|
}
|
|
19
22
|
if (!isSecureContext) {
|
|
20
|
-
throw new Error(
|
|
23
|
+
throw new Error("Trillet SDK requires a secure context (HTTPS or localhost)");
|
|
21
24
|
}
|
|
22
25
|
// Validate config
|
|
23
26
|
if (!config.connectionToken && (!config.apiKey || !config.workspaceId)) {
|
|
24
|
-
throw new Error(
|
|
27
|
+
throw new Error("Either connectionToken OR (apiKey AND workspaceId) must be provided");
|
|
25
28
|
}
|
|
26
29
|
if (!config.agentId) {
|
|
27
|
-
throw new Error(
|
|
30
|
+
throw new Error("agentId is required");
|
|
28
31
|
}
|
|
29
32
|
this.config = {
|
|
30
33
|
...config,
|
|
31
|
-
mode: config.mode ||
|
|
34
|
+
mode: config.mode || "voice", // Default to voice mode
|
|
32
35
|
};
|
|
33
36
|
this.sdk = new TrilletWebSDK();
|
|
34
37
|
this.setupEventForwarding();
|
|
35
38
|
}
|
|
36
39
|
setupEventForwarding() {
|
|
40
|
+
var _a;
|
|
37
41
|
// Forward all SDK events to agent listeners
|
|
38
|
-
this.sdk.on(
|
|
39
|
-
this.sdk.on(
|
|
40
|
-
this.sdk.on(
|
|
41
|
-
this.sdk.on(
|
|
42
|
-
this.sdk.on(
|
|
43
|
-
this.sdk.on(
|
|
44
|
-
this.sdk.on(
|
|
45
|
-
this.sdk.on(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
42
|
+
this.sdk.on("connected", () => this.emit("connected"));
|
|
43
|
+
this.sdk.on("disconnected", () => this.emit("disconnected"));
|
|
44
|
+
this.sdk.on("error", (error) => this.emit("error", error));
|
|
45
|
+
this.sdk.on("status", (status) => this.emit("status", status));
|
|
46
|
+
this.sdk.on("metadata", (metadata) => this.emit("metadata", metadata));
|
|
47
|
+
this.sdk.on("assistantStartedSpeaking", () => this.emit("assistantStartedSpeaking"));
|
|
48
|
+
this.sdk.on("assistantStoppedSpeaking", () => this.emit("assistantStoppedSpeaking"));
|
|
49
|
+
this.sdk.on("audioData", (data) => this.emit("audioData", data));
|
|
50
|
+
// Forward Room events to agent listeners
|
|
51
|
+
(_a = this.sdk
|
|
52
|
+
.getRoom()) === null || _a === void 0 ? void 0 : _a.on(RoomEvent.TranscriptionReceived, (segments, participant) => {
|
|
53
|
+
const room = this.sdk.getRoom();
|
|
54
|
+
if (!room)
|
|
55
|
+
return;
|
|
56
|
+
const participantId = (participant === null || participant === void 0 ? void 0 : participant.identity) || room.localParticipant.identity;
|
|
57
|
+
const isAgent = participantId.startsWith("agent-") ||
|
|
58
|
+
participantId.startsWith("web-agent");
|
|
59
|
+
segments.forEach((segment) => {
|
|
60
|
+
var _a;
|
|
61
|
+
if (segment.final) {
|
|
62
|
+
delete this.currentTranscript[participantId];
|
|
63
|
+
const text = segment.text.trim();
|
|
64
|
+
// Check for duplicates
|
|
65
|
+
const lastMessage = this.transcripts[this.transcripts.length - 1];
|
|
66
|
+
if (lastMessage &&
|
|
67
|
+
lastMessage.text === text &&
|
|
68
|
+
lastMessage.participantId === participantId &&
|
|
69
|
+
Date.now() - lastMessage.timestamp.getTime() < 1000) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
this.transcripts.push({
|
|
73
|
+
role: isAgent ? "assistant" : "user",
|
|
74
|
+
text,
|
|
75
|
+
isFinal: true,
|
|
76
|
+
timestamp: new Date(),
|
|
77
|
+
participantId,
|
|
78
|
+
toolUsed: text.includes("Using tool:")
|
|
79
|
+
? {
|
|
80
|
+
name: text.split("Using tool:")[1].split(" ")[1],
|
|
81
|
+
args: (_a = text.split("Arguments:")[1]) === null || _a === void 0 ? void 0 : _a.trim(),
|
|
82
|
+
status: "pending",
|
|
83
|
+
}
|
|
84
|
+
: undefined,
|
|
85
|
+
});
|
|
69
86
|
}
|
|
70
|
-
|
|
71
|
-
|
|
87
|
+
else {
|
|
88
|
+
this.currentTranscript[participantId] = segment.text;
|
|
72
89
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return data.sdk_token;
|
|
77
|
-
}
|
|
78
|
-
catch (error) {
|
|
79
|
-
console.error('Trillet SDK Authentication Error:', error);
|
|
80
|
-
throw error;
|
|
81
|
-
}
|
|
90
|
+
});
|
|
91
|
+
this.emit(RoomEvent.TranscriptionReceived, segments, participant);
|
|
92
|
+
});
|
|
82
93
|
}
|
|
83
94
|
async startCall() {
|
|
84
95
|
try {
|
|
85
|
-
//
|
|
86
|
-
const token = await this.authenticate();
|
|
87
|
-
// 2. Initialize call with LiveKit token
|
|
96
|
+
// 2. Initialize call with API KEY
|
|
88
97
|
const response = await fetch(TRILLET_API_URL.CONNECT, {
|
|
89
|
-
method:
|
|
98
|
+
method: "POST",
|
|
90
99
|
headers: {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
100
|
+
"Content-Type": "application/json",
|
|
101
|
+
Accept: "application/json",
|
|
102
|
+
"X-API_KEY": this.config.apiKey,
|
|
94
103
|
},
|
|
95
104
|
body: JSON.stringify({
|
|
96
105
|
agentId: this.config.agentId,
|
|
97
106
|
mode: this.config.mode,
|
|
98
107
|
dynamic_variables: this.config.variables || {},
|
|
99
|
-
callback_url: this.config.callbackUrl
|
|
100
|
-
})
|
|
108
|
+
callback_url: this.config.callbackUrl,
|
|
109
|
+
}),
|
|
101
110
|
});
|
|
102
111
|
if (!response.ok) {
|
|
103
112
|
let errorMessage;
|
|
104
113
|
try {
|
|
105
114
|
const errorData = await response.json();
|
|
106
|
-
errorMessage =
|
|
115
|
+
errorMessage =
|
|
116
|
+
errorData.message || errorData.error || "Unknown error";
|
|
107
117
|
}
|
|
108
118
|
catch (_a) {
|
|
109
119
|
errorMessage = await response.text();
|
|
@@ -112,36 +122,90 @@ export class TrilletAgent extends EventEmitter {
|
|
|
112
122
|
}
|
|
113
123
|
const data = await response.json();
|
|
114
124
|
if (!data.token || !data.roomName) {
|
|
115
|
-
throw new Error(
|
|
125
|
+
throw new Error("Invalid response from server: missing token or room name");
|
|
116
126
|
}
|
|
117
127
|
// Initialize call with LiveKit token
|
|
118
128
|
await this.sdk.initializeCall({
|
|
119
129
|
token: data.token,
|
|
120
|
-
wsUrl: process.env.LIVEKIT_WS_URL ||
|
|
130
|
+
wsUrl: process.env.LIVEKIT_WS_URL ||
|
|
131
|
+
"wss://trillet-ai-xdx0dw5r.livekit.cloud",
|
|
121
132
|
audioSettings: {
|
|
122
|
-
sampleRate: 48000
|
|
133
|
+
sampleRate: 48000,
|
|
123
134
|
},
|
|
124
135
|
features: {
|
|
125
|
-
enableRawAudio: this.config.mode ===
|
|
126
|
-
}
|
|
136
|
+
enableRawAudio: this.config.mode === "voice",
|
|
137
|
+
},
|
|
127
138
|
});
|
|
128
139
|
// Enable audio playback for voice mode
|
|
129
|
-
if (this.config.mode ===
|
|
140
|
+
if (this.config.mode === "voice") {
|
|
130
141
|
await this.sdk.enableAudioPlayback();
|
|
131
142
|
}
|
|
132
143
|
// Emit additional connection details
|
|
133
|
-
this.emit(
|
|
144
|
+
this.emit("connected", {
|
|
134
145
|
callId: data.callId,
|
|
135
146
|
roomName: data.roomName,
|
|
136
|
-
agent: data.agent
|
|
147
|
+
agent: data.agent,
|
|
137
148
|
});
|
|
138
149
|
}
|
|
139
150
|
catch (error) {
|
|
140
|
-
console.error(
|
|
141
|
-
this.emit(
|
|
151
|
+
console.error("Trillet SDK Error:", error);
|
|
152
|
+
this.emit("error", error instanceof Error ? error : new Error(String(error)));
|
|
142
153
|
throw error;
|
|
143
154
|
}
|
|
144
155
|
}
|
|
156
|
+
async startPublicCall() {
|
|
157
|
+
// 2. Initialize public call
|
|
158
|
+
const response = await fetch(TRILLET_API_URL.PUBLIC_CONNECT, {
|
|
159
|
+
method: "POST",
|
|
160
|
+
headers: {
|
|
161
|
+
"Content-Type": "application/json",
|
|
162
|
+
Accept: "application/json",
|
|
163
|
+
'x-workspace-id': this.config.workspaceId
|
|
164
|
+
},
|
|
165
|
+
body: JSON.stringify({
|
|
166
|
+
call_agent_id: this.config.agentId,
|
|
167
|
+
mode: this.config.mode,
|
|
168
|
+
dynamic_variables: this.config.variables || {},
|
|
169
|
+
callback_url: this.config.callbackUrl,
|
|
170
|
+
}),
|
|
171
|
+
});
|
|
172
|
+
if (!response.ok) {
|
|
173
|
+
let errorMessage;
|
|
174
|
+
try {
|
|
175
|
+
const errorData = await response.json();
|
|
176
|
+
errorMessage = errorData.message || errorData.error || "Unknown error";
|
|
177
|
+
}
|
|
178
|
+
catch (_a) {
|
|
179
|
+
errorMessage = await response.text();
|
|
180
|
+
}
|
|
181
|
+
throw new Error(`API Error: ${errorMessage}`);
|
|
182
|
+
}
|
|
183
|
+
const data = await response.json();
|
|
184
|
+
if (!data.token || !data.roomName) {
|
|
185
|
+
throw new Error("Invalid response from server: missing token or room name");
|
|
186
|
+
}
|
|
187
|
+
// Initialize call with LiveKit token
|
|
188
|
+
await this.sdk.initializeCall({
|
|
189
|
+
token: data.token,
|
|
190
|
+
wsUrl: process.env.LIVEKIT_WS_URL || "wss://trillet-ai-xdx0dw5r.livekit.cloud",
|
|
191
|
+
audioSettings: {
|
|
192
|
+
sampleRate: 48000,
|
|
193
|
+
},
|
|
194
|
+
features: {
|
|
195
|
+
enableRawAudio: this.config.mode === "voice",
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
// Enable audio playback for voice mode
|
|
199
|
+
if (this.config.mode === "voice") {
|
|
200
|
+
await this.sdk.enableAudioPlayback();
|
|
201
|
+
}
|
|
202
|
+
// Emit additional connection details
|
|
203
|
+
this.emit("connected", {
|
|
204
|
+
callId: data.callId,
|
|
205
|
+
roomName: data.roomName,
|
|
206
|
+
agent: data.agent,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
145
209
|
endCall() {
|
|
146
210
|
this.sdk.endCall();
|
|
147
211
|
}
|
|
@@ -151,6 +215,12 @@ export class TrilletAgent extends EventEmitter {
|
|
|
151
215
|
get isAssistantSpeaking() {
|
|
152
216
|
return this.sdk.isAssistantSpeaking;
|
|
153
217
|
}
|
|
218
|
+
getTranscripts() {
|
|
219
|
+
return this.transcripts;
|
|
220
|
+
}
|
|
221
|
+
getCurrentTranscript() {
|
|
222
|
+
return this.currentTranscript;
|
|
223
|
+
}
|
|
154
224
|
}
|
|
155
225
|
// Internal SDK class
|
|
156
226
|
class TrilletWebSDK extends EventEmitter {
|
|
@@ -184,7 +254,7 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
184
254
|
this.emit("connected");
|
|
185
255
|
}
|
|
186
256
|
catch (error) {
|
|
187
|
-
const errorMessage = error instanceof Error ? error.message :
|
|
257
|
+
const errorMessage = error instanceof Error ? error.message : "Failed to initialize call";
|
|
188
258
|
this.emit("error", errorMessage);
|
|
189
259
|
console.error("Call initialization failed:", error);
|
|
190
260
|
this.endCall();
|
|
@@ -251,7 +321,7 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
251
321
|
this.audioAnalyzer = {
|
|
252
322
|
calculateVolume: analyzer.calculateVolume,
|
|
253
323
|
analyser: analyzer.analyser,
|
|
254
|
-
cleanup: analyzer.cleanup
|
|
324
|
+
cleanup: analyzer.cleanup,
|
|
255
325
|
};
|
|
256
326
|
this.audioFrameCapture = requestAnimationFrame(() => this.captureAudio());
|
|
257
327
|
}
|
|
@@ -289,4 +359,7 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
289
359
|
break;
|
|
290
360
|
}
|
|
291
361
|
}
|
|
362
|
+
getRoom() {
|
|
363
|
+
return this.room;
|
|
364
|
+
}
|
|
292
365
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trillet-ai/web-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
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",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"license": "MIT",
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"eventemitter3": "^5.0.1",
|
|
35
|
-
"livekit-client": "^
|
|
35
|
+
"livekit-client": "^2.8.1"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@types/node": "^20.11.0",
|