@trillet-ai/web-sdk 1.5.0 → 1.5.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.
Files changed (2) hide show
  1. package/dist/index.js +44 -51
  2. 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'));
@@ -65,6 +64,32 @@ export class TrilletAgent extends EventEmitter {
65
64
  this.emit('toolCompleted', toolResult);
66
65
  this.emit('transcriptUpdate', this.getTranscripts());
67
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
+ });
68
93
  // Handle incoming text messages from the agent
69
94
  this.sdk.on('message', (message) => {
70
95
  var _a, _b;
@@ -79,49 +104,6 @@ export class TrilletAgent extends EventEmitter {
79
104
  this.emit('message', message);
80
105
  this.emit('transcriptUpdate', this.getTranscripts());
81
106
  });
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
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
- }
97
- const room = this.sdk.getRoom();
98
- if (!room)
99
- return;
100
- const participantId = participant.identity || room.localParticipant.identity;
101
- segments.forEach((segment) => {
102
- if (segment.final) {
103
- delete this.currentTranscript[participantId];
104
- const text = segment.text.trim();
105
- // Check for duplicates
106
- const lastMessage = this.transcripts[this.transcripts.length - 1];
107
- if (lastMessage && lastMessage.text === text && lastMessage.participantId === participantId && Date.now() - lastMessage.timestamp.getTime() < 1000) {
108
- return;
109
- }
110
- this.transcripts.push({
111
- role: 'user',
112
- text,
113
- isFinal: true,
114
- timestamp: new Date(),
115
- participantId,
116
- });
117
- this.emit('transcriptUpdate', this.getTranscripts());
118
- }
119
- else {
120
- this.currentTranscript[participantId] = segment.text;
121
- }
122
- });
123
- this.emit(RoomEvent.TranscriptionReceived, segments, participant);
124
- });
125
107
  }
126
108
  async startCall() {
127
109
  try {
@@ -254,24 +236,22 @@ export class TrilletAgent extends EventEmitter {
254
236
  return this.currentTranscript;
255
237
  }
256
238
  sendTextMessage(text) {
257
- var _a, _b;
258
239
  if (this.config.mode !== 'text') {
259
240
  console.warn('Cannot send text message in voice mode.');
260
241
  return;
261
242
  }
262
- if (!this.sdk.getRoom() || !this.sdk.isConnected) {
243
+ const room = this.sdk.getRoom();
244
+ if (!room || !this.sdk.isConnected) {
263
245
  this.emit('error', new Error('Not connected. Cannot send message.'));
264
246
  return;
265
247
  }
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 });
248
+ room.localParticipant.sendText(text);
269
249
  this.transcripts.push({
270
250
  role: 'user',
271
251
  text,
272
252
  isFinal: true,
273
253
  timestamp: new Date(),
274
- participantId: (_b = this.sdk.getRoom()) === null || _b === void 0 ? void 0 : _b.localParticipant.identity,
254
+ participantId: room.localParticipant.identity,
275
255
  });
276
256
  this.emit('transcriptUpdate', this.transcripts);
277
257
  }
@@ -367,6 +347,18 @@ class TrilletWebSDK extends EventEmitter {
367
347
  if (this.isConnected)
368
348
  this.endCall();
369
349
  });
350
+ this.room.registerTextStreamHandler('lk.transcription', async (reader, info) => {
351
+ var _a, _b;
352
+ const text = await reader.readAll();
353
+ const attributes = ((_a = reader.info) === null || _a === void 0 ? void 0 : _a.attributes) || {};
354
+ const isFinal = attributes['lk.transcription_final'] === 'true' || attributes['lk.transcribed_track_id'];
355
+ if (!info)
356
+ return;
357
+ const participant = (_b = this.room) === null || _b === void 0 ? void 0 : _b.getParticipantByIdentity(info.identity);
358
+ if (!participant)
359
+ return;
360
+ this.emit('transcript', { text, isFinal }, participant);
361
+ });
370
362
  // Audio events
371
363
  this.room.on(RoomEvent.TrackSubscribed, (track, publication) => {
372
364
  var _a;
@@ -415,7 +407,8 @@ class TrilletWebSDK extends EventEmitter {
415
407
  this.emit('metadata', event);
416
408
  break;
417
409
  case 'transcript':
418
- this.emit('transcript', event.data, participant);
410
+ // this is for agent-generated transcripts, which we are overriding with lk.transcription
411
+ // this.emit('transcript', event.data, participant);
419
412
  break;
420
413
  case 'llm_call':
421
414
  this.emit('toolStarted', { name: event.name, args: event.arguments });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trillet-ai/web-sdk",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
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",