@trillet-ai/web-sdk 1.7.1 → 1.9.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.
- package/README.md +60 -0
- package/dist/TranscriptManager-Bh-JJm_m.d.ts +15 -0
- package/dist/chunk-3MH6DJZ6.js +46 -0
- package/dist/chunk-3MH6DJZ6.js.map +1 -0
- package/dist/chunk-FSJ42TKR.js +718 -0
- package/dist/chunk-FSJ42TKR.js.map +1 -0
- package/dist/index.d.ts +101 -5
- package/dist/index.js +9 -347
- package/dist/index.js.map +1 -0
- package/dist/server.d.ts +43 -0
- package/dist/server.js +7 -0
- package/dist/server.js.map +1 -0
- package/dist/widget/index.d.ts +277 -0
- package/dist/widget/index.js +1648 -0
- package/dist/widget/index.js.map +1 -0
- package/dist/widget.umd.js +3883 -0
- package/dist/widget.umd.js.map +1 -0
- package/package.json +24 -3
- package/dist/BrowserFingerprint.d.ts +0 -46
- package/dist/BrowserFingerprint.js +0 -131
- package/dist/TranscriptManager.d.ts +0 -35
- package/dist/TranscriptManager.js +0 -63
- package/dist/TrilletWebSDK.d.ts +0 -38
- package/dist/TrilletWebSDK.js +0 -238
package/package.json
CHANGED
|
@@ -1,17 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trillet-ai/web-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.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",
|
|
7
7
|
"type": "module",
|
|
8
|
+
"unpkg": "dist/widget.umd.js",
|
|
9
|
+
"jsdelivr": "dist/widget.umd.js",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./server": {
|
|
17
|
+
"types": "./dist/server.d.ts",
|
|
18
|
+
"import": "./dist/server.js",
|
|
19
|
+
"default": "./dist/server.js"
|
|
20
|
+
},
|
|
21
|
+
"./widget": {
|
|
22
|
+
"types": "./dist/widget/index.d.ts",
|
|
23
|
+
"import": "./dist/widget/index.js",
|
|
24
|
+
"default": "./dist/widget/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./dist/widget.umd.js": "./dist/widget.umd.js"
|
|
27
|
+
},
|
|
8
28
|
"publishConfig": {
|
|
9
29
|
"access": "public"
|
|
10
30
|
},
|
|
11
31
|
"scripts": {
|
|
12
32
|
"clean": "rimraf dist",
|
|
13
|
-
"build": "npm run clean &&
|
|
14
|
-
"dev": "
|
|
33
|
+
"build": "npm run clean && tsup",
|
|
34
|
+
"dev": "tsup --watch",
|
|
15
35
|
"lint": "eslint src/**/*.{ts,tsx}",
|
|
16
36
|
"prepare": "npm run build",
|
|
17
37
|
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
|
|
@@ -47,6 +67,7 @@
|
|
|
47
67
|
"jest": "^29.7.0",
|
|
48
68
|
"rimraf": "^5.0.10",
|
|
49
69
|
"ts-jest": "^29.1.2",
|
|
70
|
+
"tsup": "^8.3.5",
|
|
50
71
|
"typescript": "^5.3.3"
|
|
51
72
|
},
|
|
52
73
|
"files": [
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Browser Fingerprinting Utility
|
|
3
|
-
* Generates a deterministic fingerprint based on stable browser/hardware characteristics.
|
|
4
|
-
* Used for demo link usage limit tracking.
|
|
5
|
-
*
|
|
6
|
-
* Strategy:
|
|
7
|
-
* 1. localStorage persistence — generate once, reuse across tabs/sessions forever
|
|
8
|
-
* 2. Stable hardware-only seed — if storage is cleared, the same hardware signals
|
|
9
|
-
* produce the same hash, so the fingerprint stays consistent
|
|
10
|
-
* 3. Server-side IP fallback — handled by the backend when no fingerprint is sent
|
|
11
|
-
*/
|
|
12
|
-
export declare class BrowserFingerprint {
|
|
13
|
-
private static cachedFingerprint;
|
|
14
|
-
/**
|
|
15
|
-
* Generate (or retrieve) a deterministic browser fingerprint.
|
|
16
|
-
* Cached in memory → localStorage → derived from stable hardware signals.
|
|
17
|
-
*/
|
|
18
|
-
static generate(): Promise<string>;
|
|
19
|
-
/**
|
|
20
|
-
* Collect only hardware / environment signals that do NOT change between
|
|
21
|
-
* tabs, sessions, or browser restarts on the same device.
|
|
22
|
-
*
|
|
23
|
-
* Deliberately excluded (volatile / anti-fingerprint randomised):
|
|
24
|
-
* - canvas.toDataURL() — randomised per origin in many browsers
|
|
25
|
-
* - font detection — noisy across rendering states
|
|
26
|
-
* - screen.availWidth/Height — changes with taskbar / dock resize
|
|
27
|
-
* - navigator.doNotTrack — user can toggle
|
|
28
|
-
* - timezoneOffset — changes with DST
|
|
29
|
-
* - navigator.userAgent — changes on browser update
|
|
30
|
-
*/
|
|
31
|
-
private static getStableSignals;
|
|
32
|
-
/**
|
|
33
|
-
* WebGL vendor + renderer string (GPU identifier, very stable)
|
|
34
|
-
*/
|
|
35
|
-
private static getWebGLFingerprint;
|
|
36
|
-
/**
|
|
37
|
-
* SHA-256 hash of the signals object → hex string
|
|
38
|
-
*/
|
|
39
|
-
private static hashComponents;
|
|
40
|
-
private static getStored;
|
|
41
|
-
private static persist;
|
|
42
|
-
/**
|
|
43
|
-
* Clear both in-memory and persisted fingerprint (useful for testing)
|
|
44
|
-
*/
|
|
45
|
-
static clearCache(): void;
|
|
46
|
-
}
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Browser Fingerprinting Utility
|
|
3
|
-
* Generates a deterministic fingerprint based on stable browser/hardware characteristics.
|
|
4
|
-
* Used for demo link usage limit tracking.
|
|
5
|
-
*
|
|
6
|
-
* Strategy:
|
|
7
|
-
* 1. localStorage persistence — generate once, reuse across tabs/sessions forever
|
|
8
|
-
* 2. Stable hardware-only seed — if storage is cleared, the same hardware signals
|
|
9
|
-
* produce the same hash, so the fingerprint stays consistent
|
|
10
|
-
* 3. Server-side IP fallback — handled by the backend when no fingerprint is sent
|
|
11
|
-
*/
|
|
12
|
-
const STORAGE_KEY = 'trillet_dfp';
|
|
13
|
-
export class BrowserFingerprint {
|
|
14
|
-
/**
|
|
15
|
-
* Generate (or retrieve) a deterministic browser fingerprint.
|
|
16
|
-
* Cached in memory → localStorage → derived from stable hardware signals.
|
|
17
|
-
*/
|
|
18
|
-
static async generate() {
|
|
19
|
-
// 1. In-memory cache (fastest)
|
|
20
|
-
if (this.cachedFingerprint) {
|
|
21
|
-
return this.cachedFingerprint;
|
|
22
|
-
}
|
|
23
|
-
// 2. Persisted in localStorage (survives tab close / browser restart)
|
|
24
|
-
const stored = this.getStored();
|
|
25
|
-
if (stored) {
|
|
26
|
-
this.cachedFingerprint = stored;
|
|
27
|
-
return stored;
|
|
28
|
-
}
|
|
29
|
-
// 3. Derive from stable hardware signals and persist
|
|
30
|
-
const signals = this.getStableSignals();
|
|
31
|
-
const fingerprint = await this.hashComponents(signals);
|
|
32
|
-
this.persist(fingerprint);
|
|
33
|
-
this.cachedFingerprint = fingerprint;
|
|
34
|
-
return fingerprint;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Collect only hardware / environment signals that do NOT change between
|
|
38
|
-
* tabs, sessions, or browser restarts on the same device.
|
|
39
|
-
*
|
|
40
|
-
* Deliberately excluded (volatile / anti-fingerprint randomised):
|
|
41
|
-
* - canvas.toDataURL() — randomised per origin in many browsers
|
|
42
|
-
* - font detection — noisy across rendering states
|
|
43
|
-
* - screen.availWidth/Height — changes with taskbar / dock resize
|
|
44
|
-
* - navigator.doNotTrack — user can toggle
|
|
45
|
-
* - timezoneOffset — changes with DST
|
|
46
|
-
* - navigator.userAgent — changes on browser update
|
|
47
|
-
*/
|
|
48
|
-
static getStableSignals() {
|
|
49
|
-
return {
|
|
50
|
-
// Hardware — fixed for a given device
|
|
51
|
-
screen: {
|
|
52
|
-
width: window.screen.width,
|
|
53
|
-
height: window.screen.height,
|
|
54
|
-
colorDepth: window.screen.colorDepth,
|
|
55
|
-
pixelRatio: window.devicePixelRatio,
|
|
56
|
-
},
|
|
57
|
-
hardwareConcurrency: navigator.hardwareConcurrency,
|
|
58
|
-
deviceMemory: navigator.deviceMemory,
|
|
59
|
-
platform: navigator.platform,
|
|
60
|
-
maxTouchPoints: navigator.maxTouchPoints,
|
|
61
|
-
// WebGL vendor/renderer — GPU-based, stable
|
|
62
|
-
webgl: this.getWebGLFingerprint(),
|
|
63
|
-
// Locale — stable unless user changes OS language or moves timezone
|
|
64
|
-
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
65
|
-
language: navigator.language,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* WebGL vendor + renderer string (GPU identifier, very stable)
|
|
70
|
-
*/
|
|
71
|
-
static getWebGLFingerprint() {
|
|
72
|
-
try {
|
|
73
|
-
const canvas = document.createElement('canvas');
|
|
74
|
-
const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
|
|
75
|
-
if (!gl)
|
|
76
|
-
return 'no-webgl';
|
|
77
|
-
const glContext = gl;
|
|
78
|
-
const debugInfo = glContext.getExtension('WEBGL_debug_renderer_info');
|
|
79
|
-
const vendor = debugInfo
|
|
80
|
-
? glContext.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL)
|
|
81
|
-
: glContext.getParameter(glContext.VENDOR);
|
|
82
|
-
const renderer = debugInfo
|
|
83
|
-
? glContext.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL)
|
|
84
|
-
: glContext.getParameter(glContext.RENDERER);
|
|
85
|
-
return `${vendor}~${renderer}`;
|
|
86
|
-
}
|
|
87
|
-
catch (_a) {
|
|
88
|
-
return 'webgl-error';
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* SHA-256 hash of the signals object → hex string
|
|
93
|
-
*/
|
|
94
|
-
static async hashComponents(components) {
|
|
95
|
-
const data = new TextEncoder().encode(JSON.stringify(components));
|
|
96
|
-
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
|
|
97
|
-
return Array.from(new Uint8Array(hashBuffer))
|
|
98
|
-
.map((b) => b.toString(16).padStart(2, '0'))
|
|
99
|
-
.join('');
|
|
100
|
-
}
|
|
101
|
-
// ── localStorage helpers ───────────────────────────────────────────
|
|
102
|
-
static getStored() {
|
|
103
|
-
try {
|
|
104
|
-
return localStorage.getItem(STORAGE_KEY);
|
|
105
|
-
}
|
|
106
|
-
catch (_a) {
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
static persist(fingerprint) {
|
|
111
|
-
try {
|
|
112
|
-
localStorage.setItem(STORAGE_KEY, fingerprint);
|
|
113
|
-
}
|
|
114
|
-
catch (_a) {
|
|
115
|
-
// Storage full or disabled — fingerprint still works in-memory
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Clear both in-memory and persisted fingerprint (useful for testing)
|
|
120
|
-
*/
|
|
121
|
-
static clearCache() {
|
|
122
|
-
this.cachedFingerprint = null;
|
|
123
|
-
try {
|
|
124
|
-
localStorage.removeItem(STORAGE_KEY);
|
|
125
|
-
}
|
|
126
|
-
catch (_a) {
|
|
127
|
-
// Ignore
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
BrowserFingerprint.cachedFingerprint = null;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
export interface ToolUsed {
|
|
2
|
-
name: string;
|
|
3
|
-
args?: any;
|
|
4
|
-
status?: 'pending' | 'success' | 'error';
|
|
5
|
-
}
|
|
6
|
-
export interface Transcript {
|
|
7
|
-
role: 'user' | 'assistant';
|
|
8
|
-
text: string;
|
|
9
|
-
isFinal: boolean;
|
|
10
|
-
timestamp: Date;
|
|
11
|
-
participantId?: string;
|
|
12
|
-
toolUsed?: ToolUsed;
|
|
13
|
-
}
|
|
14
|
-
export declare class TranscriptManager {
|
|
15
|
-
private transcripts;
|
|
16
|
-
private currentTranscript;
|
|
17
|
-
getTranscripts(): Transcript[];
|
|
18
|
-
getCurrentTranscript(): {
|
|
19
|
-
[key: string]: string;
|
|
20
|
-
};
|
|
21
|
-
addToolStarted(toolCall: {
|
|
22
|
-
name: string;
|
|
23
|
-
args: any;
|
|
24
|
-
}): void;
|
|
25
|
-
addToolCompleted(toolResult: {
|
|
26
|
-
name: string;
|
|
27
|
-
result: any;
|
|
28
|
-
isError: boolean;
|
|
29
|
-
}): void;
|
|
30
|
-
handleTranscript(participantId: string, isAgent: boolean, transcriptData: {
|
|
31
|
-
text: string;
|
|
32
|
-
isFinal: boolean;
|
|
33
|
-
}): boolean;
|
|
34
|
-
addMessage(role: 'user' | 'assistant', text: string, participantId?: string): void;
|
|
35
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
export class TranscriptManager {
|
|
2
|
-
constructor() {
|
|
3
|
-
this.transcripts = [];
|
|
4
|
-
this.currentTranscript = {};
|
|
5
|
-
}
|
|
6
|
-
getTranscripts() {
|
|
7
|
-
return this.transcripts;
|
|
8
|
-
}
|
|
9
|
-
getCurrentTranscript() {
|
|
10
|
-
return this.currentTranscript;
|
|
11
|
-
}
|
|
12
|
-
addToolStarted(toolCall) {
|
|
13
|
-
this.transcripts.push({
|
|
14
|
-
role: 'assistant',
|
|
15
|
-
text: `Using tool: ${toolCall.name}`,
|
|
16
|
-
isFinal: true,
|
|
17
|
-
timestamp: new Date(),
|
|
18
|
-
toolUsed: { ...toolCall, status: 'pending' },
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
addToolCompleted(toolResult) {
|
|
22
|
-
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);
|
|
23
|
-
if (toolTranscriptIndex > -1) {
|
|
24
|
-
const toolTranscript = this.transcripts[toolTranscriptIndex];
|
|
25
|
-
if (toolTranscript && toolTranscript.toolUsed) {
|
|
26
|
-
toolTranscript.toolUsed.status = toolResult.isError ? 'error' : 'success';
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
handleTranscript(participantId, isAgent, transcriptData) {
|
|
31
|
-
if (transcriptData.isFinal) {
|
|
32
|
-
delete this.currentTranscript[participantId];
|
|
33
|
-
const text = transcriptData.text.trim();
|
|
34
|
-
if (!text)
|
|
35
|
-
return false;
|
|
36
|
-
// Check for duplicates
|
|
37
|
-
const lastMessage = this.transcripts[this.transcripts.length - 1];
|
|
38
|
-
if (lastMessage && lastMessage.text === text && lastMessage.participantId === participantId && Date.now() - lastMessage.timestamp.getTime() < 1000) {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
this.transcripts.push({
|
|
42
|
-
role: isAgent ? 'assistant' : 'user',
|
|
43
|
-
text,
|
|
44
|
-
isFinal: true,
|
|
45
|
-
timestamp: new Date(),
|
|
46
|
-
participantId,
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
this.currentTranscript[participantId] = transcriptData.text;
|
|
51
|
-
}
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
addMessage(role, text, participantId) {
|
|
55
|
-
this.transcripts.push({
|
|
56
|
-
role,
|
|
57
|
-
text,
|
|
58
|
-
isFinal: true,
|
|
59
|
-
timestamp: new Date(),
|
|
60
|
-
participantId,
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
}
|
package/dist/TrilletWebSDK.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'eventemitter3';
|
|
2
|
-
import { Room } from 'livekit-client';
|
|
3
|
-
export interface TrilletCallConfig {
|
|
4
|
-
token: string;
|
|
5
|
-
wsUrl: string;
|
|
6
|
-
audioSettings?: {
|
|
7
|
-
sampleRate?: number;
|
|
8
|
-
inputDeviceId?: string;
|
|
9
|
-
outputDeviceId?: string;
|
|
10
|
-
};
|
|
11
|
-
mode?: 'voice' | 'text';
|
|
12
|
-
features?: {
|
|
13
|
-
enableRawAudio?: boolean;
|
|
14
|
-
enableKrispNoiseFilter?: boolean;
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
export declare class TrilletWebSDK extends EventEmitter {
|
|
18
|
-
private room;
|
|
19
|
-
isConnected: boolean;
|
|
20
|
-
isAssistantSpeaking: boolean;
|
|
21
|
-
audioAnalyzer?: {
|
|
22
|
-
calculateVolume: () => number;
|
|
23
|
-
analyser: AnalyserNode;
|
|
24
|
-
cleanup: () => Promise<void>;
|
|
25
|
-
};
|
|
26
|
-
private krispProcessor?;
|
|
27
|
-
private audioFrameCapture;
|
|
28
|
-
constructor();
|
|
29
|
-
initializeCall(config: TrilletCallConfig): Promise<void>;
|
|
30
|
-
enableAudioPlayback(): Promise<void>;
|
|
31
|
-
endCall(): void;
|
|
32
|
-
toggleMicrophone(enabled: boolean): void;
|
|
33
|
-
private setupKrispNoiseFilter;
|
|
34
|
-
private captureAudio;
|
|
35
|
-
private setupEventHandlers;
|
|
36
|
-
private handleDataEvent;
|
|
37
|
-
getRoom(): Room | undefined;
|
|
38
|
-
}
|
package/dist/TrilletWebSDK.js
DELETED
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'eventemitter3';
|
|
2
|
-
import { RemoteAudioTrack, Room, RoomEvent, Track, createAudioAnalyser, LocalAudioTrack } from 'livekit-client';
|
|
3
|
-
const decoder = new TextDecoder();
|
|
4
|
-
// Internal SDK class
|
|
5
|
-
export class TrilletWebSDK extends EventEmitter {
|
|
6
|
-
constructor() {
|
|
7
|
-
super();
|
|
8
|
-
this.isConnected = false;
|
|
9
|
-
// Audio state tracking
|
|
10
|
-
this.isAssistantSpeaking = false;
|
|
11
|
-
}
|
|
12
|
-
async initializeCall(config) {
|
|
13
|
-
var _a, _b, _c, _d;
|
|
14
|
-
try {
|
|
15
|
-
this.room = new Room({
|
|
16
|
-
audioCaptureDefaults: {
|
|
17
|
-
autoGainControl: true,
|
|
18
|
-
echoCancellation: true,
|
|
19
|
-
noiseSuppression: true,
|
|
20
|
-
channelCount: 1,
|
|
21
|
-
deviceId: (_a = config.audioSettings) === null || _a === void 0 ? void 0 : _a.inputDeviceId,
|
|
22
|
-
sampleRate: (_b = config.audioSettings) === null || _b === void 0 ? void 0 : _b.sampleRate,
|
|
23
|
-
},
|
|
24
|
-
audioOutput: {
|
|
25
|
-
deviceId: (_c = config.audioSettings) === null || _c === void 0 ? void 0 : _c.outputDeviceId,
|
|
26
|
-
},
|
|
27
|
-
});
|
|
28
|
-
this.setupEventHandlers(config);
|
|
29
|
-
this.room.prepareConnection(config.wsUrl, config.token);
|
|
30
|
-
await this.room.connect(config.wsUrl, config.token);
|
|
31
|
-
console.log('Connected to Trillet room:', this.room.name);
|
|
32
|
-
if (config.mode !== 'text') {
|
|
33
|
-
await this.room.localParticipant.setMicrophoneEnabled(true);
|
|
34
|
-
// Apply Krisp noise filter if enabled
|
|
35
|
-
if ((_d = config.features) === null || _d === void 0 ? void 0 : _d.enableKrispNoiseFilter) {
|
|
36
|
-
await this.setupKrispNoiseFilter();
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
this.isConnected = true;
|
|
40
|
-
this.emit('connected');
|
|
41
|
-
}
|
|
42
|
-
catch (error) {
|
|
43
|
-
const errorMessage = error instanceof Error ? error.message : 'Failed to initialize call';
|
|
44
|
-
this.emit('error', errorMessage);
|
|
45
|
-
console.error('Call initialization failed:', error);
|
|
46
|
-
this.endCall();
|
|
47
|
-
throw error; // Re-throw to let TrilletAgent handle it
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
async enableAudioPlayback() {
|
|
51
|
-
var _a;
|
|
52
|
-
await ((_a = this.room) === null || _a === void 0 ? void 0 : _a.startAudio());
|
|
53
|
-
}
|
|
54
|
-
endCall() {
|
|
55
|
-
var _a;
|
|
56
|
-
if (!this.isConnected)
|
|
57
|
-
return;
|
|
58
|
-
this.isConnected = false;
|
|
59
|
-
this.emit('disconnected');
|
|
60
|
-
(_a = this.room) === null || _a === void 0 ? void 0 : _a.disconnect();
|
|
61
|
-
this.isAssistantSpeaking = false;
|
|
62
|
-
if (this.audioAnalyzer) {
|
|
63
|
-
this.audioAnalyzer.cleanup();
|
|
64
|
-
this.audioAnalyzer = undefined;
|
|
65
|
-
}
|
|
66
|
-
if (this.audioFrameCapture) {
|
|
67
|
-
cancelAnimationFrame(this.audioFrameCapture);
|
|
68
|
-
this.audioFrameCapture = undefined;
|
|
69
|
-
}
|
|
70
|
-
// Cleanup Krisp processor
|
|
71
|
-
if (this.krispProcessor) {
|
|
72
|
-
this.krispProcessor = undefined;
|
|
73
|
-
}
|
|
74
|
-
this.room = undefined;
|
|
75
|
-
}
|
|
76
|
-
toggleMicrophone(enabled) {
|
|
77
|
-
if (this.isConnected && this.room) {
|
|
78
|
-
this.room.localParticipant.setMicrophoneEnabled(enabled);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
async setupKrispNoiseFilter() {
|
|
82
|
-
var _a;
|
|
83
|
-
if (!this.room)
|
|
84
|
-
return;
|
|
85
|
-
try {
|
|
86
|
-
// Dynamically import Krisp noise filter
|
|
87
|
-
const { KrispNoiseFilter, isKrispNoiseFilterSupported } = await import('@livekit/krisp-noise-filter');
|
|
88
|
-
// Check browser compatibility
|
|
89
|
-
if (!isKrispNoiseFilterSupported()) {
|
|
90
|
-
console.warn('Krisp noise filter is not supported on this browser');
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
// Get the local microphone track
|
|
94
|
-
const microphoneTrack = (_a = this.room.localParticipant.getTrackPublication(Track.Source.Microphone)) === null || _a === void 0 ? void 0 : _a.track;
|
|
95
|
-
if (microphoneTrack && microphoneTrack instanceof LocalAudioTrack) {
|
|
96
|
-
// Initialize the Krisp processor
|
|
97
|
-
this.krispProcessor = KrispNoiseFilter();
|
|
98
|
-
await microphoneTrack.setProcessor(this.krispProcessor);
|
|
99
|
-
// Enable the noise filter
|
|
100
|
-
await this.krispProcessor.setEnabled(true);
|
|
101
|
-
console.log('Krisp noise filter enabled successfully');
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
console.warn('Could not find local microphone track to apply Krisp filter');
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
catch (error) {
|
|
108
|
-
console.error('Failed to setup Krisp noise filter:', error);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
captureAudio() {
|
|
112
|
-
if (!this.isConnected || !this.audioAnalyzer)
|
|
113
|
-
return;
|
|
114
|
-
const bufferSize = this.audioAnalyzer.analyser.fftSize;
|
|
115
|
-
const audioData = new Float32Array(bufferSize);
|
|
116
|
-
this.audioAnalyzer.analyser.getFloatTimeDomainData(audioData);
|
|
117
|
-
this.emit('audioData', audioData);
|
|
118
|
-
this.audioFrameCapture = requestAnimationFrame(() => this.captureAudio());
|
|
119
|
-
}
|
|
120
|
-
setupEventHandlers(config) {
|
|
121
|
-
let wasAgentSpeaking = false;
|
|
122
|
-
if (!this.room)
|
|
123
|
-
return;
|
|
124
|
-
// Room events
|
|
125
|
-
this.room.on(RoomEvent.ParticipantDisconnected, (participant) => {
|
|
126
|
-
if ((participant === null || participant === void 0 ? void 0 : participant.identity) === 'assistant') {
|
|
127
|
-
this.endCall();
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
this.room.on(RoomEvent.Disconnected, () => {
|
|
131
|
-
if (this.isConnected)
|
|
132
|
-
this.endCall();
|
|
133
|
-
});
|
|
134
|
-
this.room.registerTextStreamHandler('lk.transcription', async (reader, info) => {
|
|
135
|
-
var _a, _b;
|
|
136
|
-
const text = await reader.readAll();
|
|
137
|
-
const attributes = ((_a = reader.info) === null || _a === void 0 ? void 0 : _a.attributes) || {};
|
|
138
|
-
const isFinal = attributes['lk.transcription_final'] === 'true';
|
|
139
|
-
if (!info)
|
|
140
|
-
return;
|
|
141
|
-
const participant = (_b = this.room) === null || _b === void 0 ? void 0 : _b.getParticipantByIdentity(info.identity);
|
|
142
|
-
if (!participant)
|
|
143
|
-
return;
|
|
144
|
-
this.emit('transcript', { text, isFinal }, participant);
|
|
145
|
-
});
|
|
146
|
-
// Audio events
|
|
147
|
-
this.room.on(RoomEvent.TrackSubscribed, (track, publication) => {
|
|
148
|
-
var _a;
|
|
149
|
-
if (track.kind === Track.Kind.Audio) {
|
|
150
|
-
if (publication.trackName === 'assistant_audio' && track instanceof RemoteAudioTrack && ((_a = config.features) === null || _a === void 0 ? void 0 : _a.enableRawAudio)) {
|
|
151
|
-
const analyzer = createAudioAnalyser(track);
|
|
152
|
-
this.audioAnalyzer = {
|
|
153
|
-
calculateVolume: analyzer.calculateVolume,
|
|
154
|
-
analyser: analyzer.analyser,
|
|
155
|
-
cleanup: analyzer.cleanup,
|
|
156
|
-
};
|
|
157
|
-
this.audioFrameCapture = requestAnimationFrame(() => this.captureAudio());
|
|
158
|
-
}
|
|
159
|
-
track.attach();
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
// Data events
|
|
163
|
-
this.room.on(RoomEvent.DataReceived, (payload, participant) => {
|
|
164
|
-
if (!(participant === null || participant === void 0 ? void 0 : participant.identity.startsWith('agent')))
|
|
165
|
-
return;
|
|
166
|
-
try {
|
|
167
|
-
const data = JSON.parse(decoder.decode(payload));
|
|
168
|
-
this.handleDataEvent(data, participant);
|
|
169
|
-
}
|
|
170
|
-
catch (error) {
|
|
171
|
-
console.error('Failed to process received data:', error);
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
this.room.on(RoomEvent.ActiveSpeakersChanged, (speakers) => {
|
|
175
|
-
const isAgentSpeaking = speakers.some((p) => p.identity.startsWith('agent'));
|
|
176
|
-
if (isAgentSpeaking && !wasAgentSpeaking) {
|
|
177
|
-
this.emit('assistantStartedSpeaking');
|
|
178
|
-
}
|
|
179
|
-
else if (!isAgentSpeaking && wasAgentSpeaking) {
|
|
180
|
-
this.emit('assistantStoppedSpeaking');
|
|
181
|
-
}
|
|
182
|
-
wasAgentSpeaking = isAgentSpeaking;
|
|
183
|
-
});
|
|
184
|
-
// Handle RPC events if available
|
|
185
|
-
// Note: LiveKit may not have specific RPC events, but we can monitor participant events
|
|
186
|
-
this.room.on(RoomEvent.ParticipantConnected, (participant) => {
|
|
187
|
-
console.log('Participant connected:', participant.identity);
|
|
188
|
-
if (participant.identity.startsWith('agent')) {
|
|
189
|
-
console.log('Agent connected, RPC communication available');
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
handleDataEvent(event, participant) {
|
|
194
|
-
switch (event.type) {
|
|
195
|
-
case 'status':
|
|
196
|
-
this.emit('status', event);
|
|
197
|
-
break;
|
|
198
|
-
case 'metadata':
|
|
199
|
-
this.emit('metadata', event);
|
|
200
|
-
break;
|
|
201
|
-
case 'transcript':
|
|
202
|
-
// this is for agent-generated transcripts, which we are overriding with lk.transcription
|
|
203
|
-
// this.emit('transcript', event.data, participant);
|
|
204
|
-
break;
|
|
205
|
-
case 'llm_call':
|
|
206
|
-
this.emit('toolStarted', { name: event.name, args: event.arguments });
|
|
207
|
-
break;
|
|
208
|
-
case 'llm_call_response':
|
|
209
|
-
this.emit('toolCompleted', {
|
|
210
|
-
name: event.details.api_name,
|
|
211
|
-
result: event.details.api_response,
|
|
212
|
-
isError: event.details.status >= 400,
|
|
213
|
-
});
|
|
214
|
-
break;
|
|
215
|
-
case 'error':
|
|
216
|
-
this.emit('toolCompleted', {
|
|
217
|
-
name: event.name,
|
|
218
|
-
result: event.details,
|
|
219
|
-
isError: true,
|
|
220
|
-
});
|
|
221
|
-
break;
|
|
222
|
-
case 'assistant_speaking_started':
|
|
223
|
-
this.isAssistantSpeaking = true;
|
|
224
|
-
this.emit('assistantStartedSpeaking');
|
|
225
|
-
break;
|
|
226
|
-
case 'assistant_speaking_ended':
|
|
227
|
-
this.isAssistantSpeaking = false;
|
|
228
|
-
this.emit('assistantStoppedSpeaking');
|
|
229
|
-
break;
|
|
230
|
-
case 'agent_message':
|
|
231
|
-
this.emit('message', event);
|
|
232
|
-
break;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
getRoom() {
|
|
236
|
-
return this.room;
|
|
237
|
-
}
|
|
238
|
-
}
|