@trillet-ai/web-sdk 1.4.0 → 1.5.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 -43
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -32,7 +32,6 @@ export class TrilletAgent extends EventEmitter {
|
|
|
32
32
|
this.setupEventForwarding();
|
|
33
33
|
}
|
|
34
34
|
setupEventForwarding() {
|
|
35
|
-
var _a;
|
|
36
35
|
// Forward all SDK events to agent listeners
|
|
37
36
|
this.sdk.on('connected', () => this.emit('connected'));
|
|
38
37
|
this.sdk.on('disconnected', () => this.emit('disconnected'));
|
|
@@ -42,10 +41,59 @@ export class TrilletAgent extends EventEmitter {
|
|
|
42
41
|
this.sdk.on('assistantStartedSpeaking', () => this.emit('assistantStartedSpeaking'));
|
|
43
42
|
this.sdk.on('assistantStoppedSpeaking', () => this.emit('assistantStoppedSpeaking'));
|
|
44
43
|
this.sdk.on('audioData', (data) => this.emit('audioData', data));
|
|
44
|
+
// Handle incoming tool usage from the agent
|
|
45
|
+
this.sdk.on('toolStarted', (toolCall) => {
|
|
46
|
+
this.transcripts.push({
|
|
47
|
+
role: 'assistant',
|
|
48
|
+
text: `Using tool: ${toolCall.name}`,
|
|
49
|
+
isFinal: true,
|
|
50
|
+
timestamp: new Date(),
|
|
51
|
+
toolUsed: { ...toolCall, status: 'pending' },
|
|
52
|
+
});
|
|
53
|
+
this.emit('toolStarted', toolCall);
|
|
54
|
+
this.emit('transcriptUpdate', this.getTranscripts());
|
|
55
|
+
});
|
|
56
|
+
this.sdk.on('toolCompleted', (toolResult) => {
|
|
57
|
+
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);
|
|
58
|
+
if (toolTranscriptIndex > -1) {
|
|
59
|
+
const toolTranscript = this.transcripts[toolTranscriptIndex];
|
|
60
|
+
if (toolTranscript && toolTranscript.toolUsed) {
|
|
61
|
+
toolTranscript.toolUsed.status = toolResult.isError ? 'error' : 'success';
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
this.emit('toolCompleted', toolResult);
|
|
65
|
+
this.emit('transcriptUpdate', this.getTranscripts());
|
|
66
|
+
});
|
|
67
|
+
this.sdk.on('transcript', (transcriptData, participant) => {
|
|
68
|
+
const participantId = participant.identity;
|
|
69
|
+
const isAgent = participantId.startsWith('agent');
|
|
70
|
+
if (transcriptData.isFinal) {
|
|
71
|
+
delete this.currentTranscript[participantId];
|
|
72
|
+
const text = transcriptData.text.trim();
|
|
73
|
+
if (!text)
|
|
74
|
+
return;
|
|
75
|
+
// Check for duplicates
|
|
76
|
+
const lastMessage = this.transcripts[this.transcripts.length - 1];
|
|
77
|
+
if (lastMessage && lastMessage.text === text && lastMessage.participantId === participantId && Date.now() - lastMessage.timestamp.getTime() < 1000) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.transcripts.push({
|
|
81
|
+
role: isAgent ? 'assistant' : 'user',
|
|
82
|
+
text,
|
|
83
|
+
isFinal: true,
|
|
84
|
+
timestamp: new Date(),
|
|
85
|
+
participantId,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
this.currentTranscript[participantId] = transcriptData.text;
|
|
90
|
+
}
|
|
91
|
+
this.emit('transcriptUpdate', this.getTranscripts());
|
|
92
|
+
});
|
|
45
93
|
// Handle incoming text messages from the agent
|
|
46
94
|
this.sdk.on('message', (message) => {
|
|
47
95
|
var _a, _b;
|
|
48
|
-
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
|
|
96
|
+
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'));
|
|
49
97
|
this.transcripts.push({
|
|
50
98
|
role: 'assistant',
|
|
51
99
|
text: message.text,
|
|
@@ -56,44 +104,6 @@ export class TrilletAgent extends EventEmitter {
|
|
|
56
104
|
this.emit('message', message);
|
|
57
105
|
this.emit('transcriptUpdate', this.getTranscripts());
|
|
58
106
|
});
|
|
59
|
-
// Forward Room events to agent listeners
|
|
60
|
-
(_a = this.sdk.getRoom()) === null || _a === void 0 ? void 0 : _a.on(RoomEvent.TranscriptionReceived, (segments, participant) => {
|
|
61
|
-
const room = this.sdk.getRoom();
|
|
62
|
-
if (!room)
|
|
63
|
-
return;
|
|
64
|
-
const participantId = (participant === null || participant === void 0 ? void 0 : participant.identity) || room.localParticipant.identity;
|
|
65
|
-
const isAgent = participantId.startsWith('agent-') || participantId.startsWith('web-agent');
|
|
66
|
-
segments.forEach((segment) => {
|
|
67
|
-
var _a;
|
|
68
|
-
if (segment.final) {
|
|
69
|
-
delete this.currentTranscript[participantId];
|
|
70
|
-
const text = segment.text.trim();
|
|
71
|
-
// Check for duplicates
|
|
72
|
-
const lastMessage = this.transcripts[this.transcripts.length - 1];
|
|
73
|
-
if (lastMessage && lastMessage.text === text && lastMessage.participantId === participantId && Date.now() - lastMessage.timestamp.getTime() < 1000) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
this.transcripts.push({
|
|
77
|
-
role: isAgent ? 'assistant' : 'user',
|
|
78
|
-
text,
|
|
79
|
-
isFinal: true,
|
|
80
|
-
timestamp: new Date(),
|
|
81
|
-
participantId,
|
|
82
|
-
toolUsed: text.includes('Using tool:')
|
|
83
|
-
? {
|
|
84
|
-
name: text.split('Using tool:')[1].split(' ')[1],
|
|
85
|
-
args: (_a = text.split('Arguments:')[1]) === null || _a === void 0 ? void 0 : _a.trim(),
|
|
86
|
-
status: 'pending',
|
|
87
|
-
}
|
|
88
|
-
: undefined,
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
this.currentTranscript[participantId] = segment.text;
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
this.emit(RoomEvent.TranscriptionReceived, segments, participant);
|
|
96
|
-
});
|
|
97
107
|
}
|
|
98
108
|
async startCall() {
|
|
99
109
|
try {
|
|
@@ -339,6 +349,18 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
339
349
|
if (this.isConnected)
|
|
340
350
|
this.endCall();
|
|
341
351
|
});
|
|
352
|
+
this.room.registerTextStreamHandler('lk.transcription', async (reader, info) => {
|
|
353
|
+
var _a, _b;
|
|
354
|
+
const text = await reader.readAll();
|
|
355
|
+
const attributes = ((_a = reader.info) === null || _a === void 0 ? void 0 : _a.attributes) || {};
|
|
356
|
+
const isFinal = attributes['lk.transcription_final'] === 'true' || attributes['lk.transcribed_track_id'];
|
|
357
|
+
if (!info)
|
|
358
|
+
return;
|
|
359
|
+
const participant = (_b = this.room) === null || _b === void 0 ? void 0 : _b.getParticipantByIdentity(info.identity);
|
|
360
|
+
if (!participant)
|
|
361
|
+
return;
|
|
362
|
+
this.emit('transcript', { text, isFinal }, participant);
|
|
363
|
+
});
|
|
342
364
|
// Audio events
|
|
343
365
|
this.room.on(RoomEvent.TrackSubscribed, (track, publication) => {
|
|
344
366
|
var _a;
|
|
@@ -357,11 +379,11 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
357
379
|
});
|
|
358
380
|
// Data events
|
|
359
381
|
this.room.on(RoomEvent.DataReceived, (payload, participant) => {
|
|
360
|
-
if ((participant === null || participant === void 0 ? void 0 : participant.identity
|
|
382
|
+
if (!(participant === null || participant === void 0 ? void 0 : participant.identity.startsWith('agent')))
|
|
361
383
|
return;
|
|
362
384
|
try {
|
|
363
385
|
const data = JSON.parse(decoder.decode(payload));
|
|
364
|
-
this.handleDataEvent(data);
|
|
386
|
+
this.handleDataEvent(data, participant);
|
|
365
387
|
}
|
|
366
388
|
catch (error) {
|
|
367
389
|
console.error('Failed to process received data:', error);
|
|
@@ -378,7 +400,7 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
378
400
|
wasAgentSpeaking = isAgentSpeaking;
|
|
379
401
|
});
|
|
380
402
|
}
|
|
381
|
-
handleDataEvent(event) {
|
|
403
|
+
handleDataEvent(event, participant) {
|
|
382
404
|
switch (event.type) {
|
|
383
405
|
case 'status':
|
|
384
406
|
this.emit('status', event);
|
|
@@ -386,6 +408,27 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
386
408
|
case 'metadata':
|
|
387
409
|
this.emit('metadata', event);
|
|
388
410
|
break;
|
|
411
|
+
case 'transcript':
|
|
412
|
+
// this is for agent-generated transcripts, which we are overriding with lk.transcription
|
|
413
|
+
// this.emit('transcript', event.data, participant);
|
|
414
|
+
break;
|
|
415
|
+
case 'llm_call':
|
|
416
|
+
this.emit('toolStarted', { name: event.name, args: event.arguments });
|
|
417
|
+
break;
|
|
418
|
+
case 'llm_call_response':
|
|
419
|
+
this.emit('toolCompleted', {
|
|
420
|
+
name: event.details.api_name,
|
|
421
|
+
result: event.details.api_response,
|
|
422
|
+
isError: event.details.status >= 400,
|
|
423
|
+
});
|
|
424
|
+
break;
|
|
425
|
+
case 'error':
|
|
426
|
+
this.emit('toolCompleted', {
|
|
427
|
+
name: event.name,
|
|
428
|
+
result: event.details,
|
|
429
|
+
isError: true,
|
|
430
|
+
});
|
|
431
|
+
break;
|
|
389
432
|
case 'assistant_speaking_started':
|
|
390
433
|
this.isAssistantSpeaking = true;
|
|
391
434
|
this.emit('assistantStartedSpeaking');
|