@trillet-ai/web-sdk 1.4.0 → 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.
Files changed (2) hide show
  1. package/dist/index.js +64 -16
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -42,10 +42,33 @@ 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
+ // 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
+ });
45
68
  // Handle incoming text messages from the agent
46
69
  this.sdk.on('message', (message) => {
47
70
  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-'));
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'));
49
72
  this.transcripts.push({
50
73
  role: 'assistant',
51
74
  text: message.text,
@@ -56,15 +79,26 @@ export class TrilletAgent extends EventEmitter {
56
79
  this.emit('message', message);
57
80
  this.emit('transcriptUpdate', this.getTranscripts());
58
81
  });
59
- // Forward Room events to agent listeners
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
60
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
+ }
61
97
  const room = this.sdk.getRoom();
62
98
  if (!room)
63
99
  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');
100
+ const participantId = participant.identity || room.localParticipant.identity;
66
101
  segments.forEach((segment) => {
67
- var _a;
68
102
  if (segment.final) {
69
103
  delete this.currentTranscript[participantId];
70
104
  const text = segment.text.trim();
@@ -74,19 +108,13 @@ export class TrilletAgent extends EventEmitter {
74
108
  return;
75
109
  }
76
110
  this.transcripts.push({
77
- role: isAgent ? 'assistant' : 'user',
111
+ role: 'user',
78
112
  text,
79
113
  isFinal: true,
80
114
  timestamp: new Date(),
81
115
  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
116
  });
117
+ this.emit('transcriptUpdate', this.getTranscripts());
90
118
  }
91
119
  else {
92
120
  this.currentTranscript[participantId] = segment.text;
@@ -357,11 +385,11 @@ class TrilletWebSDK extends EventEmitter {
357
385
  });
358
386
  // Data events
359
387
  this.room.on(RoomEvent.DataReceived, (payload, participant) => {
360
- if ((participant === null || participant === void 0 ? void 0 : participant.identity) !== 'agent')
388
+ if (!(participant === null || participant === void 0 ? void 0 : participant.identity.startsWith('agent')))
361
389
  return;
362
390
  try {
363
391
  const data = JSON.parse(decoder.decode(payload));
364
- this.handleDataEvent(data);
392
+ this.handleDataEvent(data, participant);
365
393
  }
366
394
  catch (error) {
367
395
  console.error('Failed to process received data:', error);
@@ -378,7 +406,7 @@ class TrilletWebSDK extends EventEmitter {
378
406
  wasAgentSpeaking = isAgentSpeaking;
379
407
  });
380
408
  }
381
- handleDataEvent(event) {
409
+ handleDataEvent(event, participant) {
382
410
  switch (event.type) {
383
411
  case 'status':
384
412
  this.emit('status', event);
@@ -386,6 +414,26 @@ class TrilletWebSDK extends EventEmitter {
386
414
  case 'metadata':
387
415
  this.emit('metadata', event);
388
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;
389
437
  case 'assistant_speaking_started':
390
438
  this.isAssistantSpeaking = true;
391
439
  this.emit('assistantStartedSpeaking');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trillet-ai/web-sdk",
3
- "version": "1.4.0",
3
+ "version": "1.5.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",