@trillet-ai/web-sdk 1.3.7 → 1.5.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/LICENSE +1 -1
- package/README.md +46 -8
- package/dist/index.d.ts +2 -0
- package/dist/index.js +107 -16
- package/package.json +1 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -30,6 +30,7 @@ const agent = new TrilletAgent({
|
|
|
30
30
|
apiKey: 'your-api-key',
|
|
31
31
|
workspaceId: 'your-workspace-id',
|
|
32
32
|
agentId: 'your-agent-id',
|
|
33
|
+
mode: 'voice', // 'voice' or 'text'
|
|
33
34
|
|
|
34
35
|
// Optional parameters
|
|
35
36
|
variables: {
|
|
@@ -51,11 +52,20 @@ agent.on('connected', (details) => {
|
|
|
51
52
|
});
|
|
52
53
|
|
|
53
54
|
agent.on('transcriptionReceived', (transcript) => {
|
|
54
|
-
console.log('New transcript:', transcript);
|
|
55
|
+
console.log('New voice transcript:', transcript);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
agent.on('message', (message) => {
|
|
59
|
+
console.log('New text message from agent:', message);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
agent.on('transcriptUpdate', (transcripts) => {
|
|
63
|
+
console.log('Transcript updated:', transcripts);
|
|
55
64
|
});
|
|
56
65
|
|
|
57
66
|
// Control the call
|
|
58
|
-
agent.toggleMicrophone(true); // Enable/disable microphone
|
|
67
|
+
agent.toggleMicrophone(true); // Enable/disable microphone in voice mode
|
|
68
|
+
agent.sendTextMessage("Hello, agent!"); // Send a text message in text mode
|
|
59
69
|
agent.endCall(); // End the call
|
|
60
70
|
```
|
|
61
71
|
|
|
@@ -78,7 +88,8 @@ The SDK supports two authentication methods:
|
|
|
78
88
|
```typescript
|
|
79
89
|
const agent = new TrilletAgent({
|
|
80
90
|
workspaceId: 'your-workspace-id',
|
|
81
|
-
agentId: 'your-agent-id'
|
|
91
|
+
agentId: 'your-agent-id',
|
|
92
|
+
mode: 'text' // Or 'voice'
|
|
82
93
|
});
|
|
83
94
|
await agent.startPublicCall();
|
|
84
95
|
```
|
|
@@ -99,7 +110,8 @@ The SDK supports two types of calls:
|
|
|
99
110
|
```typescript
|
|
100
111
|
const agent = new TrilletAgent({
|
|
101
112
|
workspaceId: 'your-workspace-id',
|
|
102
|
-
agentId: 'your-agent-id' // Required for both public and regular calls
|
|
113
|
+
agentId: 'your-agent-id', // Required for both public and regular calls
|
|
114
|
+
mode: 'text' // Or 'voice'
|
|
103
115
|
});
|
|
104
116
|
await agent.startPublicCall();
|
|
105
117
|
```
|
|
@@ -122,19 +134,27 @@ agent.on('error', (error) => {
|
|
|
122
134
|
});
|
|
123
135
|
|
|
124
136
|
agent.on('transcriptionReceived', (segments, participant) => {
|
|
125
|
-
// Called when new transcription is available
|
|
137
|
+
// Called when new transcription is available (voice mode)
|
|
126
138
|
});
|
|
127
139
|
|
|
128
140
|
agent.on('assistantStartedSpeaking', () => {
|
|
129
|
-
// Called when the AI agent starts speaking
|
|
141
|
+
// Called when the AI agent starts speaking (voice mode)
|
|
130
142
|
});
|
|
131
143
|
|
|
132
144
|
agent.on('assistantStoppedSpeaking', () => {
|
|
133
|
-
// Called when the AI agent stops speaking
|
|
145
|
+
// Called when the AI agent stops speaking (voice mode)
|
|
134
146
|
});
|
|
135
147
|
|
|
136
148
|
agent.on('audioData', (data) => {
|
|
137
|
-
// Raw audio data for visualization
|
|
149
|
+
// Raw audio data for visualization (voice mode)
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
agent.on('message', (message) => {
|
|
153
|
+
// Called when a text message is received from the agent (text mode)
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
agent.on('transcriptUpdate', (transcripts) => {
|
|
157
|
+
// Called whenever the transcript is updated (voice or text)
|
|
138
158
|
});
|
|
139
159
|
```
|
|
140
160
|
|
|
@@ -151,6 +171,24 @@ const allTranscripts = agent.getTranscripts();
|
|
|
151
171
|
const currentTranscript = agent.getCurrentTranscript();
|
|
152
172
|
```
|
|
153
173
|
|
|
174
|
+
## Text Mode Features
|
|
175
|
+
|
|
176
|
+
When using text mode, you can send and receive text messages.
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
// Send a message to the agent
|
|
180
|
+
agent.sendTextMessage("Hello, how are you?");
|
|
181
|
+
|
|
182
|
+
// Listen for messages from the agent
|
|
183
|
+
agent.on('message', (message) => {
|
|
184
|
+
console.log('Agent says:', message.text);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
// The full conversation transcript is available for both modes
|
|
188
|
+
const fullConversation = agent.getTranscripts();
|
|
189
|
+
console.log(fullConversation);
|
|
190
|
+
```
|
|
191
|
+
|
|
154
192
|
## Requirements
|
|
155
193
|
|
|
156
194
|
- Modern web browser with secure context (HTTPS or localhost)
|
package/dist/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export interface TrilletCallConfig {
|
|
|
15
15
|
inputDeviceId?: string;
|
|
16
16
|
outputDeviceId?: string;
|
|
17
17
|
};
|
|
18
|
+
mode?: 'voice' | 'text';
|
|
18
19
|
features?: {
|
|
19
20
|
enableRawAudio?: boolean;
|
|
20
21
|
};
|
|
@@ -49,5 +50,6 @@ export declare class TrilletAgent extends EventEmitter {
|
|
|
49
50
|
getCurrentTranscript(): {
|
|
50
51
|
[key: string]: string;
|
|
51
52
|
};
|
|
53
|
+
sendTextMessage(text: string): void;
|
|
52
54
|
}
|
|
53
55
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -42,15 +42,63 @@ export class TrilletAgent extends EventEmitter {
|
|
|
42
42
|
this.sdk.on('assistantStartedSpeaking', () => this.emit('assistantStartedSpeaking'));
|
|
43
43
|
this.sdk.on('assistantStoppedSpeaking', () => this.emit('assistantStoppedSpeaking'));
|
|
44
44
|
this.sdk.on('audioData', (data) => this.emit('audioData', data));
|
|
45
|
-
//
|
|
45
|
+
// Handle incoming tool usage from the agent
|
|
46
|
+
this.sdk.on('toolStarted', (toolCall) => {
|
|
47
|
+
this.transcripts.push({
|
|
48
|
+
role: 'assistant',
|
|
49
|
+
text: `Using tool: ${toolCall.name}`,
|
|
50
|
+
isFinal: true,
|
|
51
|
+
timestamp: new Date(),
|
|
52
|
+
toolUsed: { ...toolCall, status: 'pending' },
|
|
53
|
+
});
|
|
54
|
+
this.emit('toolStarted', toolCall);
|
|
55
|
+
this.emit('transcriptUpdate', this.getTranscripts());
|
|
56
|
+
});
|
|
57
|
+
this.sdk.on('toolCompleted', (toolResult) => {
|
|
58
|
+
const toolTranscriptIndex = this.transcripts.map((t) => { var _a, _b; return ((_a = t.toolUsed) === null || _a === void 0 ? void 0 : _a.name) === toolResult.name && ((_b = t.toolUsed) === null || _b === void 0 ? void 0 : _b.status) === 'pending'; }).lastIndexOf(true);
|
|
59
|
+
if (toolTranscriptIndex > -1) {
|
|
60
|
+
const toolTranscript = this.transcripts[toolTranscriptIndex];
|
|
61
|
+
if (toolTranscript && toolTranscript.toolUsed) {
|
|
62
|
+
toolTranscript.toolUsed.status = toolResult.isError ? 'error' : 'success';
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
this.emit('toolCompleted', toolResult);
|
|
66
|
+
this.emit('transcriptUpdate', this.getTranscripts());
|
|
67
|
+
});
|
|
68
|
+
// Handle incoming text messages from the agent
|
|
69
|
+
this.sdk.on('message', (message) => {
|
|
70
|
+
var _a, _b;
|
|
71
|
+
const agentParticipant = Array.from((_b = (_a = this.sdk.getRoom()) === null || _a === void 0 ? void 0 : _a.remoteParticipants.values()) !== null && _b !== void 0 ? _b : []).find((p) => p.identity.startsWith('agent'));
|
|
72
|
+
this.transcripts.push({
|
|
73
|
+
role: 'assistant',
|
|
74
|
+
text: message.text,
|
|
75
|
+
isFinal: true,
|
|
76
|
+
timestamp: new Date(),
|
|
77
|
+
participantId: agentParticipant === null || agentParticipant === void 0 ? void 0 : agentParticipant.identity,
|
|
78
|
+
});
|
|
79
|
+
this.emit('message', message);
|
|
80
|
+
this.emit('transcriptUpdate', this.getTranscripts());
|
|
81
|
+
});
|
|
82
|
+
this.sdk.on('transcript', (transcriptData, participant) => {
|
|
83
|
+
// Use agent-provided transcript as the source of truth
|
|
84
|
+
this.transcripts.push({
|
|
85
|
+
...transcriptData,
|
|
86
|
+
role: 'assistant',
|
|
87
|
+
participantId: participant.identity,
|
|
88
|
+
});
|
|
89
|
+
this.emit('transcriptUpdate', this.getTranscripts());
|
|
90
|
+
});
|
|
91
|
+
// Forward Room events to agent listeners for user transcription
|
|
46
92
|
(_a = this.sdk.getRoom()) === null || _a === void 0 ? void 0 : _a.on(RoomEvent.TranscriptionReceived, (segments, participant) => {
|
|
93
|
+
if (!participant || participant.identity.startsWith('agent')) {
|
|
94
|
+
// We are using agent-provided data for assistant transcripts
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
47
97
|
const room = this.sdk.getRoom();
|
|
48
98
|
if (!room)
|
|
49
99
|
return;
|
|
50
|
-
const participantId =
|
|
51
|
-
const isAgent = participantId.startsWith('agent-') || participantId.startsWith('web-agent');
|
|
100
|
+
const participantId = participant.identity || room.localParticipant.identity;
|
|
52
101
|
segments.forEach((segment) => {
|
|
53
|
-
var _a;
|
|
54
102
|
if (segment.final) {
|
|
55
103
|
delete this.currentTranscript[participantId];
|
|
56
104
|
const text = segment.text.trim();
|
|
@@ -60,19 +108,13 @@ export class TrilletAgent extends EventEmitter {
|
|
|
60
108
|
return;
|
|
61
109
|
}
|
|
62
110
|
this.transcripts.push({
|
|
63
|
-
role:
|
|
111
|
+
role: 'user',
|
|
64
112
|
text,
|
|
65
113
|
isFinal: true,
|
|
66
114
|
timestamp: new Date(),
|
|
67
115
|
participantId,
|
|
68
|
-
toolUsed: text.includes('Using tool:')
|
|
69
|
-
? {
|
|
70
|
-
name: text.split('Using tool:')[1].split(' ')[1],
|
|
71
|
-
args: (_a = text.split('Arguments:')[1]) === null || _a === void 0 ? void 0 : _a.trim(),
|
|
72
|
-
status: 'pending',
|
|
73
|
-
}
|
|
74
|
-
: undefined,
|
|
75
116
|
});
|
|
117
|
+
this.emit('transcriptUpdate', this.getTranscripts());
|
|
76
118
|
}
|
|
77
119
|
else {
|
|
78
120
|
this.currentTranscript[participantId] = segment.text;
|
|
@@ -117,6 +159,7 @@ export class TrilletAgent extends EventEmitter {
|
|
|
117
159
|
await this.sdk.initializeCall({
|
|
118
160
|
token: data.token,
|
|
119
161
|
wsUrl: process.env.LIVEKIT_WS_URL || 'wss://trillet-ai-xdx0dw5r.livekit.cloud',
|
|
162
|
+
mode: this.config.mode,
|
|
120
163
|
audioSettings: {
|
|
121
164
|
sampleRate: 48000,
|
|
122
165
|
},
|
|
@@ -176,6 +219,7 @@ export class TrilletAgent extends EventEmitter {
|
|
|
176
219
|
await this.sdk.initializeCall({
|
|
177
220
|
token: data.token,
|
|
178
221
|
wsUrl: process.env.LIVEKIT_WS_URL || 'wss://trillet-ai-xdx0dw5r.livekit.cloud',
|
|
222
|
+
mode: this.config.mode,
|
|
179
223
|
audioSettings: {
|
|
180
224
|
sampleRate: 48000,
|
|
181
225
|
},
|
|
@@ -209,6 +253,28 @@ export class TrilletAgent extends EventEmitter {
|
|
|
209
253
|
getCurrentTranscript() {
|
|
210
254
|
return this.currentTranscript;
|
|
211
255
|
}
|
|
256
|
+
sendTextMessage(text) {
|
|
257
|
+
var _a, _b;
|
|
258
|
+
if (this.config.mode !== 'text') {
|
|
259
|
+
console.warn('Cannot send text message in voice mode.');
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (!this.sdk.getRoom() || !this.sdk.isConnected) {
|
|
263
|
+
this.emit('error', new Error('Not connected. Cannot send message.'));
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const payload = { type: 'user_message', message: text };
|
|
267
|
+
const encoder = new TextEncoder();
|
|
268
|
+
(_a = this.sdk.getRoom()) === null || _a === void 0 ? void 0 : _a.localParticipant.publishData(encoder.encode(JSON.stringify(payload)), { reliable: true });
|
|
269
|
+
this.transcripts.push({
|
|
270
|
+
role: 'user',
|
|
271
|
+
text,
|
|
272
|
+
isFinal: true,
|
|
273
|
+
timestamp: new Date(),
|
|
274
|
+
participantId: (_b = this.sdk.getRoom()) === null || _b === void 0 ? void 0 : _b.localParticipant.identity,
|
|
275
|
+
});
|
|
276
|
+
this.emit('transcriptUpdate', this.transcripts);
|
|
277
|
+
}
|
|
212
278
|
}
|
|
213
279
|
// Internal SDK class
|
|
214
280
|
class TrilletWebSDK extends EventEmitter {
|
|
@@ -237,7 +303,9 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
237
303
|
this.setupEventHandlers(config);
|
|
238
304
|
await this.room.connect(config.wsUrl, config.token);
|
|
239
305
|
console.log('Connected to Trillet room:', this.room.name);
|
|
240
|
-
|
|
306
|
+
if (config.mode !== 'text') {
|
|
307
|
+
this.room.localParticipant.setMicrophoneEnabled(true);
|
|
308
|
+
}
|
|
241
309
|
this.isConnected = true;
|
|
242
310
|
this.emit('connected');
|
|
243
311
|
}
|
|
@@ -317,11 +385,11 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
317
385
|
});
|
|
318
386
|
// Data events
|
|
319
387
|
this.room.on(RoomEvent.DataReceived, (payload, participant) => {
|
|
320
|
-
if ((participant === null || participant === void 0 ? void 0 : participant.identity
|
|
388
|
+
if (!(participant === null || participant === void 0 ? void 0 : participant.identity.startsWith('agent')))
|
|
321
389
|
return;
|
|
322
390
|
try {
|
|
323
391
|
const data = JSON.parse(decoder.decode(payload));
|
|
324
|
-
this.handleDataEvent(data);
|
|
392
|
+
this.handleDataEvent(data, participant);
|
|
325
393
|
}
|
|
326
394
|
catch (error) {
|
|
327
395
|
console.error('Failed to process received data:', error);
|
|
@@ -338,7 +406,7 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
338
406
|
wasAgentSpeaking = isAgentSpeaking;
|
|
339
407
|
});
|
|
340
408
|
}
|
|
341
|
-
handleDataEvent(event) {
|
|
409
|
+
handleDataEvent(event, participant) {
|
|
342
410
|
switch (event.type) {
|
|
343
411
|
case 'status':
|
|
344
412
|
this.emit('status', event);
|
|
@@ -346,6 +414,26 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
346
414
|
case 'metadata':
|
|
347
415
|
this.emit('metadata', event);
|
|
348
416
|
break;
|
|
417
|
+
case 'transcript':
|
|
418
|
+
this.emit('transcript', event.data, participant);
|
|
419
|
+
break;
|
|
420
|
+
case 'llm_call':
|
|
421
|
+
this.emit('toolStarted', { name: event.name, args: event.arguments });
|
|
422
|
+
break;
|
|
423
|
+
case 'llm_call_response':
|
|
424
|
+
this.emit('toolCompleted', {
|
|
425
|
+
name: event.details.api_name,
|
|
426
|
+
result: event.details.api_response,
|
|
427
|
+
isError: event.details.status >= 400,
|
|
428
|
+
});
|
|
429
|
+
break;
|
|
430
|
+
case 'error':
|
|
431
|
+
this.emit('toolCompleted', {
|
|
432
|
+
name: event.name,
|
|
433
|
+
result: event.details,
|
|
434
|
+
isError: true,
|
|
435
|
+
});
|
|
436
|
+
break;
|
|
349
437
|
case 'assistant_speaking_started':
|
|
350
438
|
this.isAssistantSpeaking = true;
|
|
351
439
|
this.emit('assistantStartedSpeaking');
|
|
@@ -354,6 +442,9 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
354
442
|
this.isAssistantSpeaking = false;
|
|
355
443
|
this.emit('assistantStoppedSpeaking');
|
|
356
444
|
break;
|
|
445
|
+
case 'agent_message':
|
|
446
|
+
this.emit('message', event);
|
|
447
|
+
break;
|
|
357
448
|
}
|
|
358
449
|
}
|
|
359
450
|
getRoom() {
|